In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to use datetime package and time package in Python". In daily operation, I believe many people have doubts about how to use datetime package and time package in Python. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubt of "how to use datetime package and time package in Python". Next, please follow the editor to study!
1. Datetime package 1.timedelta (params …) Get a time increment object # coding:utf-8from datetime import timedeltaif _ _ name__ ='_ _ main__': # Common parameters hours: hour days: day seconds: second milliseconds: millisecond delta = timedelta (hours=2) print (delta) # 2:00:00 print (type (delta)) # 2.timezone (timedelta) + timedelta (params...) Create time zone object # coding:utf-8from datetime import timedelta, timezoneif _ _ name__ = ='_ _ main__': delta = timedelta (hours=2) zone = timezone (delta) # cooperate with timedelta to create time zone object print (zone) # UTC+02:00 print (type (zone)) # 3.datetime module datetime.now (timezone) get current time datetime object # coding:utf-8from datetime import timedelta, timezone Datetimeif _ _ name__ = ='_ _ main__': 'get the current time You can get the current time of the specified time zone datetime.now (timezone)''now = datetime.now () print (now) # 2022-02-23 1315 print 59 datetime.now (type (now)) # set the current time print of the specified time zone (datetime.now ((timezone (timedelta (hours=9) # 2022-02-23 14:59:59.224286+09:00datetime.strftime (fmt) ) datetime time object convert string # coding:utf-8from datetime import datetimeif _ _ name__ = ='_ _ main__': 'datetime.strftime (fmt) convert time object to string fmt: formatting standard Common format characters composed of format characters (year:% Y, month:% m, day:% D, hour:% H, minutes:% M, seconds:% S)''now = datetime.now () print (now.strftime ('% Y-%m-%d% HRV% MRV% S')) # 2022-02-23 14:04:24datetime.strptime (date_string) Fmt) string to datetime time object # coding:utf-8from datetime import datetimeif _ _ name__ = ='_ _ main__': 'datetime.strptime (date_string,fmt) to convert a string to a time object The format of date_string is required to match the fmt format standard 'time_obj = datetime.strptime (' 2022-2-22 hours,'% Ymuri% mmure% d') # datetime.strptime ('2022-2-22 hours,'% Y-%m-%d% H') there are no hours in Error date_string, while hours are required in fmt (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)) # datetime.timestamp (datetime_obj) converts the datetime time object into a second timestamp # coding:utf-8from datetime import datetimeif _ name__ = =' _ main__':''datetime.timestamp (datetime_obj) Datetime_obj:datetime time object returns float 'print (datetime.timestamp (datetime.now () # 1645598565.715datetime.fromtimestamp (t) converts a second timestamp into a datetime time object # coding:utf-8from datetime import datetime Timedelta, timezoneif _ name__ ='_ main__': 'datetime.fromtimestamp (t) t: second timestamp float Type returns: datetime time object' 'datetime_obj = datetime.fromtimestamp (1645598565.715) print (datetime_obj) # 2022-02-23 14name__ 42name__ 45.715000 print (type (datetime_obj)) # 4. Use datetime object + timedelta (params …) Perform time operations # coding:utf-8from datetime import datetime, timedelta Timezoneif _ _ name__ = ='_ main__': now = datetime.now () fmt ='% Y-%m-%d% HV% MV% S'print (now.strftime (fmt)) # 2022-02-23 15:07:01 # 3 hours after print ((now + timedelta (hours=3)). Strftime (fmt)) # 2022-02-23 18:07:01 # 3 hours before 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 # it is recommended that all parameters of timedelta use positive numbers (easy to understand). Time package 1.time.time () gets the current second timestamp # coding:utf-8import timeif _ _ name__ = ='_ main__': print (time.time ()) # 1645667203.72367242.time.localtime (second) converts seconds to time time object # coding:utf-8import timeif _ _ name__ = ='_ main__': # second is left empty The default current timestamp 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)) # print (type (T2)) # 3.time.strftime (fmt,time_obj) converts the time time object into a string # coding:utf-8import timeif _ _ name__ = ='_ _ main__': "time.strftime (fmt,time_obj) fmt: format the standard reference datetime.strftime (fmt) time_obj:time time object Leave empty the time time object "" t = time.localtime (time.time ()) + 3600) print (time.strftime ('% Y-%m-%d% HV% MV% S') # 2022-02-24 10:16:17 print (time.strftime ('% Y-%m-%d% HV% MV% S), t)) # 2022-02-24 11:16:174.time.strptime (time_string) Fmt) converts a string into a time time object # coding:utf-8import timeif _ _ name__ = ='_ _ main__': "time.strptime (time_string,fmt) reference datetime.strptime (date_string,fmt) time_string: time string fmt: formatting standard"fmt ='% Y-%m-%d% Hlav% MRV% SRT = 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) 5.time.sleep (second) hibernate second seconds # coding:utf-8import timeif _ _ name__ = ='_ main__': print (time.time ()) # 1645670183.6567423 time.sleep (2) print (time.time ()) # 1645670185.6708047 to this The study on "how to use datetime package and time package in Python" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.