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

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

Share

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

Editor to share with you how to use the time operation datetime in Python, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Python provides time module and datetime module in the built-in library for time operations.

Time module learns the methods related to the conversion between structured time, timestamp and string time.

Datetime module provides a variety of processing times and dates, simple and convenient.

Python also provides calendar module for general calendar-related methods, time zone information zoneinfo module, and third-party library arrow and dateutil modules for time processing.

1. Overview of datetime module

The datetime module not only supports mathematical operations of time and date, but also provides attribute-related methods for efficient output formatting and operation.

The datetime module divides the date and time into two types of perceptual objects and simple objects according to the time zone information.

Perceptual time objects: make full use of applied calculations and local conditions to accurately locate a point in time, usually used to represent a fixed point in time where there is no interpretation space.

Simple time object: it does not contain local information, everything depends on the time calculated by the specific program, ignores realistic factors, and usually represents UTC world standard time.

Datetime provides many classes that make it easier for us to retrieve information about perceptual time objects.

Objects of type date are simple.

Objects of time and datetime types can be perceptual or simple

Timedelta objects are neither perceptual nor simple.

2. Datetime components

The datetime module differs from time in that it mainly provides classes for operating dates and times.

By viewing the datetime module file, it mainly provides 6 classes including processing time, date and time zone related classes.

The class name description datetime.date is used to represent the date, the commonly used attributes are: year, month and daydatetime.time are used to represent the time, and the common attributes are: hour, minute, second, microseconddatetime.datetime are used to represent the date and time datetime.timedelta is used to represent the time interval between two instances of date, time, and datetime. The smallest unit can reach the abstract base class of the information object related to the microsecond datetime.tzinfo time zone. They are used by the datetime and time classes to provide custom timing adjustments. The new function in datetime.timezonePython 3.2implements the tzinfo abstract base class, which represents a fixed offset from UTC.

Datetime module related class diagram:

The objects of the classes in the datetime module are immutable types

Date and time are the two classes that we use most every day.

3. Datetime constant

In the datetime.pyi file, we know that the detetime module defines two constant dat

The constant name describes the minimum value of the year allowed by the datetime.MINYEARdatetime.date or datetime.datetime object, while the value is the maximum allowed by the 1datetime.MAXYEARdatetime.date or datetime.datetime object, which is only 99994. Common methods of datetime

The datetime module provides more operation time and date operations than the time module. Here are some common methods.

Method uses datetime.datetime.timestamp () to convert the datetime object into the form of a timestamp datetime.datetime.now () format the output current system time datetime.datetime.timedelta ([hours,days]) sets the offset of a property datetime.datetime.strptime (date_str) Format) convert the time string to a datetime object datetime.date.fromtimestamp (timestamp), format the timestamp as a date datetime.date.today (), return the local date datetime.time.strftime (fromat), return the local time format output datetime.time.dst (), return the local time zone. If tzinfo is none, the nonedatetime.tzinfo.utcoffset (dt) setting time zone difference is returned, the east is positive, the timedelta object datetime.tzinfo.dst (dt) is returned, and the daylight saving time adjustment is returned to a timedelta object 5.

We learn from the common methods of the datetime module. Let's judge whether the input date is yesterday or today is Ture, otherwise it is false.

Def check_current_date (sw_date): cur_date_list = time.strftime ("% Y/%m/%d" Time.localtime () .split ('/') today = datetime.datetime.now () offset = datetime.timedelta (days=-1) yes_data = today+offset yes_data_list = yes_data.strftime ("% Y/%m/%d"). Split ('/') data_list = cur_date_list + yes_data_list print ("data_list", data_list) seps = ['/','-' '_'] for sep in seps: date_parts= sw_date.split (sep) print ("cur_date_list = {}, arg sw_date = {}" .format (data_list, sw_date)) if len (date_parts) = = 3: for data in date_parts: if data not in data_list: return False return True return False

Let's test the results:

Print ("current date:", datetime.datetime.now ()) print ("the day before testing:", check_current_date ("2021-10-29")) print ("Test current date:", check_current_date ("2021-10-30")) above are all the contents of this article entitled "how to manipulate datetime in Python". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, 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

Development

Wechat

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

12
Report