Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use time and datetime modules in python

Shulou Source: shulou.com Published: 2022-06-03 04:43:58 10月02日 Update

This article will explain in detail how to use the time and datetime modules in python. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Import time

# x=time.time () # from the year it was born

# print (time.time ())

# y=int (xAccord 3600 Compact 24Accord 365)

# print (y)

# print (time.time ()) # get the timestamp (from 1970 to the current time (in seconds))

# print (time.timezone) # difference between standard time and local time (in seconds), China is 8 hours earlier than utc

# print (time.altzone) # difference between daylight saving time and utc

# print (time.daylight) # whether to use daylight saving time (0 means unused)

# print (time.sleep (2)) # wait 2 seconds before output

Timestamps are converted to tuples

# print (time.gmtime ()) # converted to utc standard time (if no timestamp is passed, the default current time is converted to utc)

# print (time.localtime ()) # convert to local time utc+8

# (tm_year=2017, tm_mon=11, tm_mday=1, tm_hour=14, tm_min=10

# tm_sec=42, tm_wday=2, tm_yday=305, tm_isdst=0)

Convert tuples to timestamps (in seconds)

# x=time.localtime ()

# print (time.mktime (x))

Convert tuples to formatted strings

# x=time.localtime ()

# print (time.strftime ("% Y/%m/%d% H:%M:%S", x))

# strftime ("format", struct_time)-> "formatted string" #% Y=x.tm_year (automatic acquisition)

Convert formatted strings to tuples

# time.strptime ("2017-11:01 14:55:26", "Y-%m:%d H:%M:%S")

# print (time.strptime ("2017-11:01 14:55:26", "Y-%m:%d H:%M:%S"))

# strptime ("formatted string", "format")-"struct_time #% yearly 2017 to get the previous string data one by one in format"

Special method format

# time.asctime () # tuple converted to string

# time.ctime () # convert timestamp to string

# a b d H:%M:%S Y)

Import datetime

# print (datetime.datetime.now ()) # get the current time

Print (datetime.datetime.now () + datetime.timedelta (3)) # the current time is postponed by 3 days

Print (datetime.datetime.now () + datetime.timedelta (- 3)) # the current time is advanced by 3 days

Print (datetime.datetime.now () + datetime.timedelta (hours=3)) # time after 3 hours

Print (datetime.datetime.now () + datetime.timedelta (hours=-3)) # 3 hours ago

Print (datetime.datetime.now () + datetime.timedelta (minutes=30))

Print (datetime.datetime.now () + datetime.timedelta (minutes=-30))

# c_time=datetime.datetime.now ()

# print (c_time.replace (minute=3,hour=2)) # replacement of time

Convert to datetime mode

Import datetime

Time1 = "2017-11-18 17:43:43"

Time2 = time.strptime (time1, "% Y-%m-%d% H:%M:%S")

Time3 = time.mktime (time2)

Print (time3)

Print (type (time3))

X = datetime.date.fromtimestamp (time3) # format the output of the year, month and day in the timestamp and convert it to the datetime.date data type

Print (x)

Print (type (x))

X1=datetime.datetime.strptime (time1, "% Y-%m-%d% H:%M:%S") # converted to datetime.datetime data type

Print (x1)

Print (type (x1))

This is the end of this article on "how to use time and datetime modules in python". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Time format character string hour data article module unit daylight saving time difference form more standard standard time type output good practical Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei OPPO Reno Xiaomi vpn NVidia