Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use time and datetime modules in python

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report