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 datetime Library in Python

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

In this issue, the editor will bring you about how to use the datetime library in Python. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

The datetime module contains functions and classes for date and time parsing, formatting, and operation

time

The time value can be represented by the time class, and the time instance contains hour, minute, second, millisecond properties, as well as time zone information. The time instance only saves the time value, which has nothing to do with the date. The precision of time is subtle.

Import datetime

Def time_study ():

T = datetime.time ()

Print (t)

T = datetime.time (0,2,3)

Print (t)

T = datetime.time (1,2,3, microsecond=2)

Print (t)

Print (t.microsecond)

Print (t.tzinfo)

Print ("Earliest:", datetime.time.min)

Print ("Latest:", datetime.time.max)

Print ("Resolution:", datetime.time.resolution) date

Calendar date values can be represented by the date class, and the date instance contains year, month, and day properties. You can easily create the current date using the class method today (). The following are several ways to show the creation date

Use fixed valu

Use timestamp

Use the replace method

Def date_study ():

Date = datetime.date (1,1,1)

Print (date)

Print (date.toordinal ())

Import time

Ts = time.time ()

Date = datetime.date.fromtimestamp (ts)

Print (date)

Date = datetime.date.today ()

Print (date)

Date_2 = date.replace (year=2019)

Print (date_2)

Today = datetime.date.today ()

Print (today)

Print ('ctime:', today.ctime ())

Print ('ordinal:', today.toordinal ())

Tt = today.timetuple ()

Print ('timetuple: tm_year =', tt.tm_year)

Print ("Earliest:", datetime.date.min)

Print ("Latest:", datetime.date.max)

Print ("Resolution:", datetime.date.resolution) timedeltas

Two datetime objects or datetime objects and timedelta can get other dates by arithmetic operations. Subtract two date to get timedelta. Timedelta is stored internally in days, seconds, and microseconds.

Def timedelta_study ():

Print ('hours:', datetime.timedelta (hours=10))

Print ('days:', datetime.timedelta (days=1, seconds=100))

Delta = datetime.timedelta (days=1, seconds=100)

Print ('total seconds:', delta.total_seconds ()) date arithmetic

Dates support standard arithmetic operators. The following example shows the use of timedelta to generate new dates and the subtraction of two dates to get timedelta.

Def date_arithmetic_study ():

Today = datetime.date.today ()

One_day = datetime.timedelta (days=1)

Yesterday = today-one_day

Tommorow = today + one_day

Print ('today:', today)

Print ('yesterday:', yesterday)

Print ('tommorow:', tommorow)

Print ('tommorow-yesterday:', tommorow-yesterday) date comparison

Date and time instances support standard comparison operators to determine which date is earlier or later

Def date_compare_study ():

Tweak 1 = datetime.time (1,2,3)

Tweak 2 = datetime.time (4,5,6)

Print (twee1)

Print (twee2)

Print ('twee1

< t_2 :', t_1 < t_2) d_1 = datetime.date.today() d_2 = datetime.date.today() + datetime.timedelta(days=1) print (d_1) print (d_2) print ('d_1 >

Data 2:', date 1 > date 2) date and time merge

The datetime class combines date and time components, and like date, there are several convenient class methods to create datetime instances.

Def combine_date_and_time ():

Print ('Now:', datetime.datetime.now ())

Print ('Today:', datetime.datetime.today ())

D = datetime.datetime.now ()

Print ('datetime Year:', getattr (d,' year'))

Print ('datetime Hour:', getattr (d,' hour'))

T = datetime.time (1,2,3)

D = datetime.date.today ()

Dt = datetime.datetime.combine (d, t)

Print (dt) date formatting and parsing

Datetime default string representation uses ISO-8601 format. Strftime: converts datetime to specified format strptime: converts specified format string to datetime object

Def format_and_parse ():

Dt = datetime.datetime.now ()

Print (dt)

Dt_format ='% Y-%m-%d% HRV% MVA% S'

Dt_str = dt.strftime (dt_format)

Print (dt_str)

Dt_new = datetime.datetime.strptime (dt_str, dt_format)

Print (dt_new.strftime (dt_format))

Print ('{:% Y-%m-%d} '.format (dt)) symbol describes% Y complete year, with century% m month% d (zero fill)% H hour (24:00 system)% M minutes% S seconds% w week% j of the year the above is the editor shared for you how to use the datetime library in Python, if you happen to have similar doubts, you can refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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

Internet Technology

Wechat

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

12
Report