1.Python有三個時間處理的模塊datetime,date苇侵,time.
1.1 datetime企锌、date撕攒、time都提供了strftime()方法格式化strftime()
格式化 |
解釋 |
格式化 |
解釋 |
%y |
兩位數(shù)的年份表示(00-99) |
%B |
本地完整的月份名稱 |
%Y |
四位數(shù)的年份表示(000-9999) |
%c |
本地相應(yīng)的日期表示和時間表示 |
%m |
月份(01-12) |
%j |
年內(nèi)的一天(001-366) |
%d |
月內(nèi)中的一天(0-31) |
%p |
本地A.M.或P.M.的等價符 |
%H |
24小時制小時數(shù)(0-23) |
%U |
一年中的星期數(shù)(00-53)星期天為星期的開始 |
%I |
12小時制小時數(shù)(01-12) |
%w |
星期(0-6)萍鲸,星期天為星期的開始 |
%M |
分鐘數(shù)(00=59) |
%W |
一年中的星期數(shù)(00-53)星期一為星期的開始 |
%S |
秒(00-59 ) |
%x |
本地相應(yīng)的日期表示 |
%a |
本地簡化星期名稱 |
%X |
本地相應(yīng)的時間表示 |
%A |
本地完整星期名稱 |
%Z |
當(dāng)前時區(qū)的名稱 |
%b |
本地簡化的月份名稱 |
%% |
%號本身 |
1.2 常見日期輸出方式
1.2.1 datetime
In [15]:import datetime
In [16]:datetime.datetime.now()
Out[16]: datetime.datetime(2016, 8, 14, 10, 34, 24, 230000)
1.2.2 timestamp
In [17]:import time
In [18]:time.time()
Out[18]: 1471142250.306
1.2.3 time tuple
In [19]:import time
In [20]:time.localtime()
Out[20]: time.struct_time(tm_year=2016, tm_mon=8, tm_mday=14,
tm_hour=10, tm_min=38, tm_sec=42, tm_wday=6, tm_yday=227, tm_isdst=0)
1.2.4 string
In [21]:import datetime
In [22]:datetime.datetime.now().strftime("%Y-%m-%d %H:%M%S")
Out[22]: '2016-08-14 10:5115'
1.2.5 date
In [23]:import datetime
In [24]:datetime.datetime.now().date()
Out[24]: datetime.date(2016, 8, 14)
2.1 datetime基本操作
2.1.1 獲取當(dāng)前datetime
In [25]:import datetime
In [26]:datetime.datetime.now()
Out[26]: datetime.datetime(2016, 8, 14, 10, 57, 27, 783000)
2.1.2 獲取當(dāng)前date
In [27]:import datetime
In [28]:datetime.date.today()
Out[28]: datetime.date(2016, 8, 14)
2.1.3 獲取明天/前N天
In [29]:import datetime
In [30]:datetime.date.today()+datetime.timedelta(days=1)
Out[30]: datetime.date(2016, 8, 15)
2.1.4 三天前
In [31]:import datetime
In [32]:datetime.date.today()-datetime.timedelta(days=3)
Out[32]: datetime.date(2016, 8, 11)
2.1.5 獲取當(dāng)天開始和結(jié)束時間(00:00:00 23:59:59)
In [33]:datetime.datetime.combine(datetime.date.today(), datetime.time.min)
Out[33]: datetime.datetime(2016, 8, 14, 0, 0)
In [34]:datetime.datetime.combine(datetime.date.today(), datetime.time.max)
Out[34]: datetime.datetime(2016, 8, 14, 23, 59, 59, 999999)
2.1.6 獲取兩個datetime的時間差
In [36]:(datetime.datetime(2017,1,13,12,0,0)-datetime.datetime.now())
Out[36]: datetime.timedelta(152, 3104, 273000)
In [37]:(datetime.datetime(2017,1,13,12,0,0)-datetime.datetime.now()).total_seconds()
Out[37]: 13135887.501
2.1.7 獲取本周/本月/上月最后一天
#本周
In [40]:today = datetime.date.today()
In [40]:today
Out[40]: datetime.date(2016, 8, 14)
In [41]:sunday = today+datetime.timedelta(6-today.weekday())
In [41]:sunday
Out[42]: datetime.date(2016, 8, 14)
#本月
In [43]:import calendar
In [44]:today = datetime.date.today()
In [45]: today
Out [45]: today
(0, 31)
In [46]:last_day_num = calendar.monthrange(today.year, today.month)
In [47]:last_day = datetime.date(today.year, today.month, last_day_num[1])
Out[48]: datetime.date(2016, 8, 31)
#獲取上個月的最后一天(可能跨年)
In [49]:import datetime
In [50]:today = datetime.date.today()
In [51]:today
Out[51]: datetime.date(2016, 8, 14)
In [52]:first = datetime.date(day=1,month=today.month,year=today.year)
In [53]:first
Out[53]: datetime.date(2016, 8, 1)
In [54]:lastMonth = first-datetime.timedelta(days=1)
In [55]:lastMonth
Out[55]: datetime.date(2016, 7, 31)
3 關(guān)系之間的轉(zhuǎn)換 Datetime / String / timestamp / time tuple
3.1datetime <=> string
3.1.1 datetime -> string
In [56]:import datetime
In [57]:datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
Out[57]: '2016-08-14 17:26:42'
3.1.2 string -> datetime
In [72]:import datetime
In [73]:datetime.datetime.strptime("2016-08-14 12:35:57","%Y-%m-%d %H:%M:%S")
Out[73]: datetime.datetime(2016, 8, 14, 12, 35, 57)
3.2 datetime <=> timetuple
3.2.1 datetime -> timetuple
In [74]:import datetime
In [75]:datetime.datetime.now().timetuple()
Out[75]: time.struct_time(tm_year=2016, tm_mon=8, tm_mday=14, tm_hour=17,
tm_min=31, tm_sec=58, tm_wday=6, tm_yday=227, tm_isdst=-1)
3.3 datetime <=> date
3.3.1 datetime -> date
In [76]:import datetime
In [77]:datetime.datetime.now().date()
Out[77]: datetime.date(2016, 8, 14)
3.3.2 datetime -> date
In [78]:import datetime
In [79]:datetime.date.today()
Out[79]: datetime.date(2016, 8, 14)
In [80]:today = datetime.date.today()
today
Out[80]: datetime.date(2016, 8, 14)
In [81]:datetime.datetime.combine(today,datetime.time())
Out[81]: datetime.datetime(2016, 8, 14, 0, 0)
In [82]:datetime.datetime.combine(today,datetime.time.min)
Out[82]: datetime.datetime(2016, 8, 14, 0, 0)
3.4 datetime <=> timestamp
3.4.1datetime -> timestamp
In [83]:import datetime
In [84]:now = datetime.datetime.now()
In [85]:now
Out[85]: datetime.datetime(2016, 8, 14, 17, 40, 44, 757000)
In [86]:timestamp = time.mktime(now.timetuple())
In [87]:timestamp
Out[87]: 1471167644.0
3.4.2 timestamp -> datetime
In [88]:import datetime
In [89]:datetime.datetime.fromtimestamp(1421077403.0)
Out[89]: datetime.datetime(2015, 1, 12, 23, 43, 23)