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

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

Share

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

This article is about how python deals with date and time. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

What are the common date operations in development?

Get the current time

Get the number of seconds of the system (since Epoch time)

Conversion between date and seconds

Get calendar, etc.

Date formatted display output

These are all very common.

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

Import timeimport calender, let's take a look at these two modules. Time built-in module #! / usr/bin/env python#-*-coding: utf-8-*-# @ Time: 2021-11-10 22:49 # @ Author: LeiXueWei# @ CSDN/Juejin/Wechat: Raytheon Committee # @ XueWeiTag: CodingDemo# @ File: _ _ init__.py.py# @ Project: helloimport time# calculates how many seconds have elapsed since zero time 19700101 Accurate to microseconds ticks= time.time () print ("ticks=", ticks) # to get the current time print (time.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:

Calender built-in module #! / usr/bin/env python#-*-coding: utf-8-*-# @ Time: 2021-11-10 22:49 # @ Author: LeiXueWei# @ CSDN/Juejin/Wechat: Ray academic Committee # @ XueWeiTag: CodingDemo# @ File: calendardemo.py# @ Project: helloimport calendarcal = calendar.month (2021, 11) print ("cal:", cal)

It has been exported for a month so far, and this is not available in Java's Calendar. It's too direct.

Date formatting processing

Here we use the strftime (str from time) of the time module:

# the first parameter is in format The second parameter is time time.strftime ("Y-%m-%d H:%M:%S Z" Gmtime) #! / 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 early morning) # gmtime = time.gmtime (sec) print ("gmtime:", gmtime) # GMTprint ("type:" Type (gmtime)) print (time.strftime ("% b% d% Y% H:%M:%S", gmtime)) print (time.strftime ("% Y-%m-%d% H:%M:%S", gmtime)) print (time.strftime ("% 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", localtime) print (time.strftime ("% Y-%m-%d% H:%M:%S% Z") Localtime) # print date plus time zone # try other formats print (time.strftime ("% D", localtime)) print (time.strftime ("% T", localtime))

Explain a little bit:

% Y-%m-%d% H:%M:%S% Z corresponds to

Year 4 digits-month-date hour: minutes: seconds time zone information

% b is the three-letter English output month, such as Jan/Feb, etc.

The following is the running result:

Thank you for reading! This is the end of the article on "how python deals with date and time". 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, 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