导入时间工具

  1. import time
  2. import calendar
  3. import datetime
  4. from dateutil import parser

时间戳转日期

  1. def timestamp_datetime(tick, format='%Y%m%d'):
  2. tick = time.localtime(tick)
  3. dt = time.strftime(format, tick)
  4. return dt

日期转时间戳

  1. def datetime_timestamp(dt, format='%Y%m%d'):
  2. tick = time.mktime(time.strptime(dt, format))
  3. return int(tick)

日期转化(diff)

  1. def datetime_shift(date, days, format='%Y%m%d'):
  2. tick = datetime_timestamp(date, format)
  3. tick = tick + days * 86400
  4. date = timestamp_datetime(tick, format)
  5. return date

这个月第1天

  1. def curr_month_first_day(date):
  2. date = date.replace('-', '')[0:8]
  3. time = datetime.date(int(date[0:4]), int(date[4:6]), int(date[6:8])) # 年,月,日
  4. first_day = datetime.date(time.year, time.month, 1)
  5. first_day = str(first_day).replace('-', '')
  6. return first_day

上个月最后一天

  1. def last_month_last_day(date):
  2. date = date.replace('-', '')[0:8]
  3. time = datetime.date(int(date[0:4]), int(date[4:6]), int(date[6:8])) # 年,月,日
  4. first_day = datetime.date(time.year, time.month, 1)
  5. pre_month = first_day - datetime.timedelta(days=1) # timedelta是一个不错的函数
  6. pre_month = str(pre_month).replace('-', '')
  7. return pre_month

上个月第1天

  1. def last_month_first_day(date):
  2. date = date.replace('-', '')[0:8]
  3. time = datetime.date(int(date[0:4]), int(date[4:6]), int(date[6:8])) # 年,月,日
  4. first_day = datetime.date(time.year, time.month, 1)
  5. pre_month = first_day - datetime.timedelta(days=1)
  6. first_day_of_pre_month = datetime.date(pre_month.year, pre_month.month, 1)
  7. first_day_of_pre_month = str(first_day_of_pre_month).replace('-', '')
  8. return first_day_of_pre_month

下个月第1天

  1. def next_month_first_day(date):
  2. date = date.replace('-', '')[0:8]
  3. time = datetime.date(int(date[0:4]), int(date[4:6]), int(date[6:8])) # 年,月,日
  4. first_day = datetime.date(time.year, time.month, 1)
  5. days_num = calendar.monthrange(first_day.year, first_day.month)[1] # 获取一个月有多少天
  6. first_day_of_next_month = first_day + datetime.timedelta(days=days_num) # 当月的最后一天只需要days_num-1即可
  7. first_day_of_next_month = str(first_day_of_next_month).replace('-', '')
  8. return first_day_of_next_month

返回两个时间点之间的日期集合

  1. def date_collection(strStart, strEnd, format='%Y%m%d'):
  2. ds = parser.parse(strStart)
  3. de = parser.parse(strEnd)
  4. dates = []
  5. while True:
  6. if (de - ds).days < 0:
  7. break
  8. dates.append(ds.strftime(format))
  9. ds = ds + datetime.timedelta(days=1)
  10. return dates

获取星座

  1. # 获取星座
  2. def get_constellation(birthday):
  3. month = int(birthday.replace('-', '')[4:6])
  4. date = int(birthday.replace('-', '')[6:9])
  5. dates = (21, 20, 21, 21, 22, 22, 23, 24, 24, 24, 23, 22)
  6. constellations = ("摩羯座", "水瓶座", "双鱼座", "白羊座", "金牛座", "双子座", "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座", "摩羯座")
  7. if date < dates[month - 1]:
  8. return constellations[month - 1]
  9. else:
  10. return constellations[month]