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 libraries in Python

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

Share

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

Today, I will talk to you about how to use time and datetime libraries in Python. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.

Time stamp

The timestamp refers to the total number of seconds since 00:00:00 GMT on January 1, 1970 (08:00:00 Beijing time, January 1, 1970). Note: currently, the maximum timestamp supported in Python is 32535244799 (3001-01-01 15:59:59)

There are many functions under the time block of Python that can convert common date formats. For example, the function time.time () is used to get the current timestamp. For example:

Import time time.time () # timestamps of the current time returned in 2000 are all 10-bit 1557212908.183994timestamps to obtain millisecond timestamps (crawler parameters are basically encrypted with 14-bit timestamps) int (round (time.time () * 1000)) 1557212908183

When writing code, it often involves the conversion of time, date and timestamp.

"date of type str converted to timestamp"

The time of import time # character type t = '2019-05-07 15 time.strptime 08Y-%m-%d 28 characters # converted to a time array timeArray = time.strptime (t, "% Y-%m-%d% H:%M:%S") print (timeArray) # timeArray can be changed into a timestamp timeStamp = int (time.mktime (timeArray)) print (timeStamp) # 138141960 as follows: time.struct_time (tm_year=2019, tm_mon=5) Tm_mday=7, tm_hour=15, tm_min=8, tm_sec=28, tm_wday=1, tm_yday=127, tm_isdst=-1) 20191557212908

String format change

# "2019-05-07 15:08:28" was changed to "15:08:28 on 2019-5-7" # first converted to a time array import timestr_time = "2019-05-07 15:08:28" time_array = time.strptime (str_time, "% Y-%m-%d% H:%M:%S") other_way_time = time.strftime ("% Y/%m/%d% H:%M:%S") Time_array) print (other_way_time) # 2019-05-07 15:08:28now = datetime.datetime.now () otherStyleTime = now.strftime ("% Y--%m--%d% H:%M:%S") print (otherStyleTime)

"timestamp turns time and date"

Import time import datetime # uses timetimeStamp = 1557212908timeArray = time.localtime (timeStamp) otherStyleTime = time.strftime ("% Y--%m--%d% H:%M:%S" TimeArray) print (otherStyleTime) # 2019 Muhashi 05Muhashi 07 15Group08Vog28 # use datetimetimeStamp = 1557212908dateArray = datetime.datetime.fromtimestamp (timeStamp) otherStyleTime = dateArray.strftime ("% Y--%m--%d% H:%M:%S") print (otherStyleTime) # 2019Mashi 05Muhash07 08VaV28# use datetime Specify the utc time, with a difference of 8 hours timeStamp = 1557212908dateArray = datetime.datetime.utcfromtimestamp (timeStamp) otherStyleTime = dateArray.strftime ("% Y--%m--%d% H:%M:%S") print (otherStyleTime) # 20119 Kuhmei 05Muffin 07 07:08:28Datetime

Datetime module is encapsulated on the basis of time module, providing more and more useful classes for us to use, such as date, time, datetime, timedelta, tzinfo. But for more flexible processing time, it's best to learn the essence of the time module and the datetime module.

Date class

Date class: mainly used to deal with year, month and day.

The constructor of the date class is as follows: datetime.date (year, month, day)

Example:

From datetime import dated = date (1999, 9, 29) print (d) print ('year:', d.year) print (' month:', d.month) print ('day:', d.day)

Specific output:

1999-09-29year: 1999month: 9day: 29time class

Time class: mainly used to deal with hours, minutes, seconds.

The constructor of the time class is as follows: datetime.time (hour [, minute [, second [, microsecond [, tzinfo])

Example:

From datetime import timet = time (22,45,59) print (t) print ('hour:', t.hour) print (' minute:', t.minute) print ('second:', t.second) print (' microsecond:', t.microsecond) # microsecond print ('format output:', t.strftime ('% Hsaw% MRV% S')) print ('Earliest:', datetime.time.min) print ('Latest:', datetime.time.max)

Output:

22:45:59hour: 22minute: 45second: 59microsecond: 0 formatted output: 22:45:59Earliest: 00:00:00Latest: 23:59:59.999999datetime subclass

The datetime subclass is a combination of the date object and the time object and is a subclass of datetime, similar to its methods.

Its constructor is as follows:

Datetime.datetime (year,month,day [, hour [, minute [, second [, microsecond [, tzinfo])

The meaning of each parameter is the same as that in the constructor of date and time, so pay attention to the range of parameter values.

"get current date and time"

> from datetime import datetime > now = datetime.now () # get the current datetime > print (now) 2019-05-07 16 purge 28purl 07.198690 > print (type (now)) timedelta class

The timedelta class is used to calculate the difference between two datetime objects. You can add, subtract, multiply and divide, and its instantiation method is very similar to the datetime object. This class contains the following properties:

(1) days: days.

(2) microseconds: number of microseconds (> = 0 and = 0 and

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