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 deal with python date and time

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

Share

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

This article introduces how to deal with the date and time of python. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

What other date operations are commonly used in development?

Time zone conversion display

Date formatting

Conversion between seconds and date and string

We often use it, such as globalized business to display different times (formats, etc.) according to different customers.

In python, there are two main modules that cover common date processing.

Import timeimport calender, let's take a look at these two modules. Type conversion in time processing: struct_time vs str

A time is created in Python. Specifically, a tuple of 9 elements is needed to create a struct_time.

The asctime function helps us format this type of time into a string.

#! / usr/bin/env python#-*-coding: utf-8-*-# @ Time: 22:49 on 2021-11-10 # @ Author: LeiXueWei# @ CSDN/Juejin/Wechat: Ray Learning Committee # @ XueWeiTag: CodingDemo# @ File: createtime.py# @ Project: time.struct_time (tm_year=2021, tm_mon=11, tm_mday=10, tm_hour=22, tm_min=55, tm_sec=11, tm_wday=16, tm_yday=16, tm_isdst=16) the9fields = (2021, 11, 10) 22, 55, 11, 16, 16, 16) fixed = time.struct_time (the9fields) print ("fixed time:", fixed) print ("type:", type (fixed)) result = time.asctime (the9fields) # similar to struct_time A tuple parameter consisting of 9 elements is required. Print ("asctime:", result) print ("type:", type (result)) localtime = time.localtime () print ("localtime:", localtime) print ("type:", type (localtime)) print ("asctime:", time.asctime (localtime))

The running effect is as follows:

This ticks is the cumulative number of seconds calculated from zero time.

You can run this program every second, each ticks value plus 1 (approximate)

Specify input to construct the time:

#! / usr/bin/env python#-*-coding: utf-8-*-# @ Time: 22:49 on 2021-11-10 # @ Author: LeiXueWei# @ CSDN/Juejin/Wechat: Ray Learning Committee # @ XueWeiTag: CodingDemo# @ File: createtime.py# @ Project: time.struct_time (tm_year=2021, tm_mon=11, tm_mday=10, tm_hour=22, tm_min=55, tm_sec=11, tm_wday=16, tm_yday=16 Tm_isdst=16) fixed = time.struct_time ((2021, 11, 10, 22, 55, 11, 16, 16, 16) print ("fixed time:", fixed)

The running effect is as follows:

Time and string conversion #! / usr/bin/env python#-*-coding: utf-8-*-# @ Time: 22:49 on 2021-11-10 # @ Author: LeiXueWei# @ CSDN/Juejin/Wechat: Ray Learning Committee # @ XueWeiTag: CodingDemo# @ File: createtime2.py# @ Project: helloimport timesec = one hour after the beginning of the Epoch (GMT 19700101 am) # gmtime = time.gmtime (sec) print ("gmtime:" Gmtime) # GMTprint ("type:", type (gmtime)) print (time.strftime ("b% d% Y% H:%M:%S", gmtime)) print ("% Y-%m-%d% H:%M:%S% Z", gmtime) # print date plus time zone print ("*" * 16) localtime = time.localtime (sec) print ("localtime:", localtime) # Local time print ("type:" Type (localtime)) print (time.strftime ("% b% d% Y% H:%M:%S", localtime)) print (time.strftime ("% Y-%m-%d% H:%M:%S% Z", localtime)) # print date plus time zone # try other formats print ("% D", localtime)) print (time.strftime ("% T", localtime))

The following is the running result:

For the time format function (strftime), it does not care which time zone your incoming time (struct_time) is, and it is also correct to output it to you.

But when we write the program to get the data, we must return the time zone information to the client, or the UI side, and finally adjust the display by the client local time zone setting.

Finally, the date text is converted to a date (struct_time).

#! / usr/bin/env python#-*-coding: utf-8-*-# @ Time: 2021-11-10 7:49 # @ Author: LeiXueWei# @ CSDN/Juejin/Wechat: Ray academic Committee # @ XueWeiTag: CodingDemo# @ File: createtime4.py# @ Project: helloimport timeprint ("strptime1:", time.strptime ("Jan 01 1970 09:00:00", "b% d% Y% H:%M:%S") print ("strptime2:" Time.strptime ("1970-01-01 09:00:00", "% Y-%m-%d% H:%M:%S") print ("strptime3:", time.strptime ("1970-01-01 09:00:00 CST", "% Y-%m-%d% H:%M:%S% Z"))

The following is the running result:

On how to carry out python date and time processing to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can 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