import datetime
from datetime import timedelta
now = datetime.datetime.now()
#今天today = now
#昨天yesterday = now - timedelta(days=1)
#明天tomorrow = now + timedelta(days=1)
#當前季度
now_quarter = now.month / 3 if now.month % 3 == 0 else now.month / 3 + 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)
python 時間處理
django,pytz環境下處理時區問題 datetime obj.replace tzinfo pytz.utc astimezone pytz.timezone settings.time zone 建構函式 date類表示乙個日期。日期由年 月 日組成 地球人都知道 date類的建構函式如下 ...
Python時間處理
使用pandas的read csv函式,parse dates 直接將檔案中的一列解析為時間,index col 將一列作為索引,時間列作為索引時,索引列型別為datetime64。在此dataframe新增一行新的時間資料 重取樣指的是將時間序列從乙個頻率轉換到另乙個頻率的過程將高頻率資料聚合到低...
Python 時間處理
1.時間字串 時間戳 timestr 2017 06 06 10 00 00 seconds time.mktime time.strptime timestr,y m d h m s 將時間元組轉換成時間戳 time.mktime timetuple 將時間戳轉會為時間元組 time.localt...