python this week, last week, this month, last month, this season, last season, this year, last year

#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)<br><br>#当前季度

 

# The first day and the last day of the week 
this_week_start = now - timedelta (Days = now.weekday ()) 
this_week_end = now + timedelta (Days = 6- now.weekday ()) 
 
# Last week, the first and last days 
last_week_start = now - timedelta (Days = now.weekday () + 7 ) 
last_week_end = now - timedelta (Days = now.weekday () + 1 ) 
 
# the first day and the last day of this month 
this_month_start = datetime.datetime (now.year, now .month, 1 ) 
this_month_end = datetime.datetime (now.year, now.month + 1, 1) - timedelta (Days = 1 ) 
 
# on the first day and last day of the month 
last_month_end = this_month_start - timedelta (Days = 1 ) 
last_month_startA datetime.datetime = (last_month_end.year, last_month_end.month,. 1 ) 
 
# season first and last 
month The = (now.month -. 1) - (now.month -. 1) +. 1. 3% 
this_quarter_start = A datetime.datetime (now.year, month the,. 1 ) 
this_quarter_end = a datetime.datetime (now.year, month the +. 3,. 1) - timedelta (Days =. 1 ) 
 
# on the first and last quarter 
last_quarter_end = this_quarter_start - timedelta (days = 1 ) 
last_quarter_start = A datetime.datetime (last_quarter_end.year, last_quarter_end.month - 2,. 1 ) 
 
# year first and last 
this_year_start = A datetime.datetime (now.year,. 1,. 1 ) 
this_year_endDatetime.datetime = (now.year + 1, 1, 1) - timedelta (Days = 1 ) 
 
# the first day and the last day of last year 
last_year_end = this_year_start - timedelta (Days = 1 ) 
last_year_start = datetime.datetime (last_year_end.year, 1, 1)

 

Guess you like

Origin www.cnblogs.com/HugJun/p/11936722.html