Python標准庫-datatime和time
作者:尹正傑
版權聲明:原創作品,謝絕轉載!否則將追究法律責任。
一.標准庫datatime
1>.datatime模塊
#!/usr/bin/env python #_*_coding:utf-8_*_ #@author :yinzhengjie #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/ #EMAIL:y1053419035@qq.com import datetime # 返回本地時區當前時間的datetime對象 print(datetime.datetime.today()) #返回當前時間的datetime對象,時間到微秒,如果tz為None,返回和today()一樣 print(datetime.datetime.now()) #沒有時區的當前時間 print(datetime.datetime.utcnow()) #從一個時間戳返回一個datetime對象 print(datetime.datetime.fromtimestamp) print(datetime.datetime.fromtimestamp(int(1559225186))) #返回一個到微秒的時間戳 print(datetime.datetime.now().timestamp()) #構造方法,year、month、day、hour、minute、second、microsecond,取datetime對象的年月日時分秒及微秒 print(datetime.datetime(2018, 9, 17, 10, 30, 43, 79043)) #返回星期的天,周一0,周日6 print(datetime.datetime.now().weekday()) #返回星期的天,周一1,周日7 print(datetime.datetime.now().isoweekday()) #返回日期date對象 print(datetime.datetime.now().date()) #返回時間time對象 print(datetime.datetime.now().time()) #修改並返回新的時間 print(datetime.datetime.now()) print(datetime.datetime.now().replace(2018,6,18)) #返回一個三元組(年,周數,周的天) print(datetime.datetime.now()) print(datetime.datetime.now().isocalendar()) #以上代碼執行結果如下: 2019-05-30 22:14:20.461607 2019-05-30 22:14:20.461607 2019-05-30 14:14:20.461607 <built-in method fromtimestamp of type object at 0x00000000587F2D90> 2019-05-30 22:06:26 1559225660.461607 2018-09-17 10:30:43.079043 3 4 2019-05-30 22:14:20.461607 2019-05-30 22:14:20.461607 2018-06-18 22:14:20.461607 2019-05-30 22:14:20.461607 (2019, 22, 4)
2>.日期格式化
#!/usr/bin/env python #_*_coding:utf-8_*_ #@author :yinzhengjie #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/ #EMAIL:y1053419035@qq.com """ 日期格式化* 類方法strptime(date_string, format) ,返回datetime對象 對象方法strftime(format) ,返回字符串 字符串format函數格式化 """ import datetime
dt = datetime.datetime.strptime("30/05/19 16:30", "%d/%m/%y %H:%M") print(dt.strftime("%Y-%m-%d %H:%M:%S")) print("{0:%Y}/{0:%m}/{0:%d} {0:%H}::{0:%M}::{0:%S}".format(dt)) #以上代碼執行結果如下: 2019-05-30 16:30:00 2019/05/30 16::30::00
3>.timedelta對象
#!/usr/bin/env python #_*_coding:utf-8_*_ #@author :yinzhengjie #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/ #EMAIL:y1053419035@qq.com """ 日期格式化* 類方法strptime(date_string, format) ,返回datetime對象 對象方法strftime(format) ,返回字符串 字符串format函數格式化 """ import datetime h = datetime.timedelta(hours=24) res1 = datetime.datetime.now() res2 = datetime.datetime.now() - h print(res1) print(res2) print((datetime.datetime.now() - res2).total_seconds()) #以上代碼執行結果如下: 2019-05-30 22:25:01.440269 2019-05-29 22:25:01.440269 86400.0
二.標准庫time
#!/usr/bin/env python #_*_coding:utf-8_*_ #@author :yinzhengjie #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/ #EMAIL:y1053419035@qq.com """ time time.sleep(secs) 將調用線程掛起指定的秒數 """ import time,datetime res1 = datetime.datetime.now() time.sleep(5) res2 = datetime.datetime.now() print(res1) print(res2) #以上代碼執行結果如下: 2019-05-30 22:27:46.400704 2019-05-30 22:27:51.400990