Python时间计算:当天、前一天、月初、月末、季初、季末

Python时间计算:当天、前一天、月初、月末、季初、季末

import datatime
date = datetime.datetime.now()

当天:

newdate = datetime.datetime.now()
condtions = {‘datadate’: newdate}

昨天:

newdate = date + datetime.timedelta(days=-1)
condtions = {‘datadate’: newdate}

本周初:

newdate = now - datetime.timedelta(days=now.weekday())
condtions = {‘datadate’: newdate}

本周末:

newdate = now + datetime.timedelta(days=6 - now.weekday())
condtions = {‘datadate’: newdate}

上周初:

newdate = now - datetime.timedelta(days=now.weekday() + 7)
condtions = {‘datadate’: newdate}

上周末:

newdate = now - datetime.timedelta(days=now.weekday() + 1)
condtions = {‘datadate’: newdate}

月初:

newdate = date.replace(day=1)
condtions = {‘datadate’: newdate}

月末:

year = date.year
month = date.month
a, b = calendar.monthrange(year, month)           # a,b——weekday的第一天是星期几(0-6对应星期一到星期天)和这个月的所有天数
newdate = datetime.datetime(year=year, month=month, day=b) # 构造本月月末datetime
condtions = {‘datadate’: newdate}

上月初:

date_now = date.replace(day=1)
date_now = date_now + datetime.timedelta(days=-1)
newdate = datetime.datetime(date_now.year, date_now.month, 1)
condtions = {‘datadate’: newdate}

上月末:

date_now = date.replace(day=1)
newdate = date_now + datetime.timedelta(days=-1)
condtions = {‘datadate’: newdate}

年初:

newdate = date.replace(month=1, day=1)
condtions = {‘datadate’: newdate}

年末:

newdate = date.replace(month=12, day=31)
condtions = {‘datadate’: newdate}

去年初:

newdate = date.replace(month=1, day=1)
newdate = newdate + datetime.timedelta(days=-1)
newdate = datetime.datetime(newdate.year, 1, 1)
condtions = {‘datadate’: newdate}

去年末:

newdate = date.replace(month=1, day=1)
newdate = newdate + datetime.timedelta(days=-1)
condtions = {‘datadate’: newdate}

季初:

month = (date.month - 1) - (date.month - 1) % 3 + 1
newdate = datetime.datetime(date.year, month, 1)
condtions = {‘datadate’: newdate}

季末:

month = (date.month - 1) - (date.month - 1) % 3 + 1
if month == 10:
  newdate = datetime.datetime(date.year + 1, 1, 1) + datetime.timedelta(days=-1)
else:
  newdate = datetime.datetime(date.year, month + 3, 1) + datetime.timedelta(days=-1)
condtions = {‘datadate’: newdate}

上季初:

month = (date.month - 1) - (date.month - 1) % 3 + 1
newdate = datetime.datetime(date.year, month, 1)
newdate = newdate + datetime.timedelta(days=-1)
newdate = datetime.datetime(newdate.year, newdate.month - 2, 1)
condtions = {‘datadate’: newdate}

上季末:

month = (date.month - 1) - (date.month - 1) % 3 + 1 # 10
newdate = datetime.datetime(date.year, month, 1)
newdate = newdate + datetime.timedelta(days=-1)
condtions = {‘datadate’: newdate}

半年初:

month = (date.month - 1) - (date.month - 1) % 6 + 1
newdate = datetime.datetime(date.year, month, 1)
condtions = {‘datadate’: newdate}

半年末:

month = (date.month - 1) - (date.month - 1) % 6 + 1
if month == 7:
  newdate = datetime.datetime(date.year + 1, 1, 1) + datetime.timedelta(days=-1)
else:
  newdate = datetime.datetime(date.year, month + 6, 1) + datetime.timedelta(days=-1)
condtions = {‘datadate’: newdate}

上个半年初:

month = (date.month - 1) - (date.month - 1) % 6 + 1
newdate = datetime.datetime(date.year, month, 1)
newdate = newdate + datetime.timedelta(days=-1)
newdate = datetime.datetime(newdate.year, newdate.month - 5, 1)
condtions = {‘datadate’: newdate}

上个半年末:

month = (date.month - 1) - (date.month - 1) % 6 + 1
newdate = datetime.datetime(date.year, month, 1)
newdate = newdate + datetime.timedelta(days=-1)
condtions = {‘datadate’: newdate}

月平均值:

ms_newdate = date.replace(day=1)
me_newdate = date
new_date = (ms_newdate, me_newdate)

季平均值:

month = (date.month - 1) - (date.month - 1) % 3 + 1
ss_newdate = datetime.datetime(date.year, month, 1)
se_newdate = date
new_date = (ss_newdate, se_newdate)

半年平均值:

month = (date.month - 1) - (date.month - 1) % 6 + 1
hs_newdate = datetime.datetime(date.year, month, 1)
he_newdate = date
new_date = (hs_newdate, he_newdate)

年均值:

ys_newdate = date.replace(month=1, day=1)
ye_newdate = date
new_date = (ys_newdate, ye_newdate)

if condtions:
  query_res = Entry.objects.filter(**condtions).filter().exclude()
if new_date:
  query_res = Entry.objects.filter(datadate__range=new_date).filter().exclude()

你可能感兴趣的:(python,内置模块)