您好,登錄后才能下訂單哦!
這篇文章主要介紹“Python中的datetime包與time包怎么用”,在日常操作中,相信很多人在Python中的datetime包與time包怎么用問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Python中的datetime包與time包怎么用”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
# coding:utf-8 from datetime import timedelta if __name__ == '__main__': # 常用參數 hours:小時 days:天 seconds:秒 milliseconds:毫秒 delta = timedelta(hours=2) print(delta) # 2:00:00 print(type(delta)) # <class 'datetime.timedelta'>
# coding:utf-8 from datetime import timedelta, timezone if __name__ == '__main__': delta = timedelta(hours=2) zone = timezone(delta) #配合timedelta創建時區對象 print(zone) # UTC+02:00 print(type(zone)) # <class 'datetime.timezone'>
datetime.now(timezone) 獲取當前時間datetime對象 # coding:utf-8 from datetime import timedelta, timezone, datetime if __name__ == '__main__': ''' 獲取當前時間,可以獲取指定時區的當前時間 datetime.now(timezone) ''' now = datetime.now() print(now) # 2022-02-23 13:59:59.224286 print(type(now)) # <class 'datetime.datetime'> # 設置指定時區的當前時間 print(datetime.now((timezone(timedelta(hours=9))))) # 2022-02-23 14:59:59.224286+09:00
# coding:utf-8 from datetime import datetime if __name__ == '__main__': ''' datetime.strftime(fmt) 將時間對象轉換成字符串 fmt:格式化標準,由格式符組成 常用格式符(年:%Y,月:%m,日:%D,時:%H,分:%M,秒:%S) ''' now = datetime.now() print(now.strftime('%Y-%m-%d %H:%M:%S')) # 2022-02-23 14:04:24
# coding:utf-8 from datetime import datetime if __name__ == '__main__': ''' datetime.strptime(date_string,fmt) 將字符串轉換成時間對象,要求date_string的格式完全匹配fmt格式化標準 ''' time_obj = datetime.strptime('2022-2-22', '%Y-%m-%d') # datetime.strptime('2022-2-22', '%Y-%m-%d %H') Error date_string 中不存在小時而fmt中要求有小時 print(datetime.strptime('2022-2-22 14', '%Y-%m-%d %H')) # 2022-02-22 14:00:00 print(time_obj) # 2022-02-22 00:00:00 print(type(time_obj)) # <class 'datetime.datetime'>
# coding:utf-8 from datetime import datetime if __name__ == '__main__': ''' datetime.timestamp(datetime_obj) datetime_obj:datetime 時間對象 返回 float ''' print(datetime.timestamp(datetime.now())) # 1645598565.715
# coding:utf-8 from datetime import datetime, timedelta, timezone if __name__ == '__main__': ''' datetime.fromtimestamp(t) t:秒級時間戳 float類型 返回:datetime時間對象 ''' datetime_obj = datetime.fromtimestamp(1645598565.715) print(datetime_obj) # 2022-02-23 14:42:45.715000 print(type(datetime_obj)) # <class 'datetime.datetime'>
# coding:utf-8 from datetime import datetime, timedelta, timezone if __name__ == '__main__': now = datetime.now() fmt = '%Y-%m-%d %H:%M:%S' print(now.strftime(fmt)) # 2022-02-23 15:07:01 # 3小時后時間 print((now + timedelta(hours=3)).strftime(fmt)) # 2022-02-23 18:07:01 # 3小時前時間 print((now - timedelta(hours=3)).strftime(fmt)) # 2022-02-23 12:07:01 print((now + timedelta(hours=-3)).strftime(fmt)) # 2022-02-23 12:07:01 # 建議timedelta的參數都使用正數(容易理解)
# coding:utf-8 import time if __name__ == '__main__': print(time.time()) # 1645667203.7236724
# coding:utf-8 import time if __name__ == '__main__': # second 不填,則默認當前的時間戳 t = time.localtime(time.time()) t2 = time.localtime() print(t) # time.struct_time(tm_year=2022, tm_mon=2, tm_mday=24, tm_hour=10, tm_min=10, tm_sec=8, tm_wday=3, tm_yday=55, tm_isdst=0) print(t2) # time.struct_time(tm_year=2022, tm_mon=2, tm_mday=24, tm_hour=10, tm_min=10, tm_sec=8, tm_wday=3, tm_yday=55, tm_isdst=0) print(type(t)) # <class 'time.struct_time'> print(type(t2)) # <class 'time.struct_time'>
# coding:utf-8 import time if __name__ == '__main__': """ time.strftime(fmt,time_obj) fmt:格式化標準 參考 datetime.strftime(fmt) time_obj:time時間對象,不填默認是當前日期的time時間對象 """ t = time.localtime(time.time() + 3600) print(time.strftime('%Y-%m-%d %H:%M:%S')) # 2022-02-24 10:16:17 print(time.strftime('%Y-%m-%d %H:%M:%S', t)) # 2022-02-24 11:16:17
# coding:utf-8 import time if __name__ == '__main__': """ time.strptime(time_string,fmt) 參考 datetime.strptime(date_string,fmt) time_string:時間字符串 fmt:格式化標準 """ fmt = '%Y-%m-%d %H:%M:%S' t = time.strftime(fmt, time.localtime()) print(t) # 2022-02-24 10:25:17 print(time.strptime(t, fmt)) # time.struct_time(tm_year=2022, tm_mon=2, tm_mday=24, tm_hour=10, tm_min=25, tm_sec=40, tm_wday=3, tm_yday=55, tm_isdst=-1)
# coding:utf-8 import time if __name__ == '__main__': print(time.time()) # 1645670183.6567423 time.sleep(2) print(time.time()) # 1645670185.6708047
到此,關于“Python中的datetime包與time包怎么用”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。