#coding=utf-8 import datetime from datetime import timedelta now = datetime.datetime.now() #今天 today = now #昨天 yesterday = now - timedelta(days=1) #明天 tomorrow = now + timedelta(days=1) #本周第一天和最后一天 this_week_start = now - timedelta(days=now.weekday()) this_week_end = now + timedelta(days=6-now.weekday()) #上周第一天和最后一天 last_week_start = now - timedelta(days=now.weekday()+7) last_week_end = now - timedelta(days=now.weekday()+1) #本月第一天和最后一天 this_month_start = datetime.datetime(now.year, now.month, 1) this_month_end = datetime.datetime(now.year, now.month + 1, 1) - timedelta(days=1) #上月第一天和最后一天 last_month_end = this_month_start - timedelta(days=1) last_month_start = datetime.datetime(last_month_end.year, last_month_end.month, 1) #本季第一天和最后一天 month = (now.month - 1) - (now.month - 1) % 3 + 1 this_quarter_start = datetime.datetime(now.year, month, 1) this_quarter_end = datetime.datetime(now.year, month + 3, 1) - timedelta(days=1) #上季第一天和最后一天 last_quarter_end = this_quarter_start - timedelta(days=1) last_quarter_start = datetime.datetime(last_quarter_end.year, last_quarter_end.month - 2, 1) #本年第一天和最后一天 this_year_start = datetime.datetime(now.year, 1, 1) this_year_end = datetime.datetime(now.year + 1, 1, 1) - timedelta(days=1) #去年第一天和最后一天 last_year_end = this_year_start - timedelta(days=1) last_year_start = datetime.datetime(last_year_end.year, 1, 1)
import datetime from datetime import timedelta today = datetime.date.today() quarter = (today.month-1) // 3 + 1 '{}Q{}'.format(today.year, quarter) # out: '2019Q1' # 本月(月份) today = datetime.date.today() today.month # out: 2 # 本月(年月) today = datetime.date.today() datetime.datetime.strftime(today, "%Y-%m") # out: '2019-02' # 上月 today = datetime.date.today() last_month = today + datetime.timedelta(days=-today.day) # last_month.month # 月份,out: 1 datetime.datetime.strftime(last_month,"%Y-%m") # out: '2019-01' # 上N个月 from dateutil.relativedelta import relativedelta # 需要引入新的包 today = datetime.date.today() last_2_month = today + relativedelta(months=-2) # 上两个月,上N个月参数为(months=-N) datetime.datetime.strftime(last_2_month, "%Y-%m") # out: '2018-12' # 当年1月 today = datetime.date.today() str(today.year) +'-01' # out: '2019-01' # ======================================= # 关于天 # ======================================= # 今天(天数) today = datetime.date.today() today.day # 今天(日期) today.isoformat() # out: '2019-02-22' # 昨天 today = datetime.date.today() yesterday = today + datetime.timedelta(days=-1) yesterday.isoformat() # out: '2019-02-21' # 明天 today = datetime.date.today() yesterday = today + datetime.timedelta(days=1) yesterday.isoformat() # out: '2019-02-23' # 本周第一天 today = datetime.date.today() week_start_day = today - datetime.timedelta(days=today.weekday()) week_start_day.isoformat() # '2019-02-18',周一 # 本周最后一天 today = datetime.date.today() week_end_day = today + datetime.timedelta(days=6-today.weekday()) week_end_day.isoformat() # '2019-02-24',周天 # 本月第一天 today = datetime.date.today() month_start_day = today - datetime.timedelta(days=today.day-1) month_start_day.isoformat() # '2019-02-01',当月第一天 # 本月最后一天 from dateutil.relativedelta import relativedelta # 引入新的包 today = datetime.date.today() # 必须先置为首日,直接用relativedelta(months=1,days=-today.day+1)会有错误结果 month_end_day = (today + datetime.timedelta(days=-today.day+1)) + relativedelta(months=1,days=-1) month_end_day.isoformat() # '2019-02-28',当月最后一天 # 本季度第一天 today = datetime.date.today() quarter_start_day = datetime.date(today.year,today.month - (today.month - 1) % 3, 1) quarter_start_day.isoformat() # '2019-01-01' ,本季度第一天 # 本季度最后一天 from dateutil.relativedelta import relativedelta # 引入新的包 today = datetime.date.today() quarter_end_day = datetime.date(today.year,today.month - (today.month - 1) % 3 +2, 1) + relativedelta(months=1,days=-1) quarter_end_day.isoformat() # '2019-03-31' ,本季度最后一天 # 本年第一天 today = datetime.date.today() year_start_day = datetime.date(today.year, 1, 1) year_start_day.isoformat() # '2019-01-01' ,本年第一天 # 本年最后一天 today = datetime.date.today() year_end_day = datetime.date(today.year+1, 1, 1) - datetime.timedelta(days=1) year_end_day.isoformat() # '2019-12-31' ,本年最后一天
获取上一季度最后一天
from datetime import datetime def getlastseason(today): today = datetime.strptime(today, '%Y%m%d') quarter = (today.month - 1) / 3 + 1 if quarter == 1: return datetime(today.year - 1, 12, 31).strftime('%Y%m%d') elif quarter == 2: return datetime(today.year, 3, 31).strftime('%Y%m%d') elif quarter == 3: return datetime(today.year, 6, 30).strftime('%Y%m%d') else: return datetime(today.year, 9, 30).strftime('%Y%m%d')