Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities.

This paper has participated inProject DigginTo win the creative gift package and challenge the creative incentive money.

Review and

Python provides the time module and the Datetime module in the time manipulation built-in library.

  • Time module of structured time and timestamp and string time for mutual conversion method learning
  • The DateTime module provides a simple and convenient way to handle multiple types of times and dates

Python also provides the Calendar module for common calendar-related methods, the ZoneInfo module for time zone information, and the arrow and Dateutil modules for time processing

In this installment, we will learn about datetime module classes and methods. Let’s go~

1. Datetime module overview

The DateTime module not only supports time and date mathematics, but also provides property-specific methods for efficient output formatting and manipulation

The DateTime module divides the date and time into two types of aware and simple objects based on the time zone information it contains

  • Perceptual time object: the full use of applied calculation, as well as the local actual situation to accurately locate the point in time, usually used to represent a fixed point in time without explanatory space
  • Simple time objects: they contain no local information and depend on the point in time calculated by a particular program, regardless of reality, and usually represent UTC world Standard time

Datetime provides a number of classes that make it easier to retrieve information about aware time objects.

  • Objects of type date are simple
  • Objects of type time and datetime can be aware or simple
  • A Timedelta object is neither aware nor simple

2. Datetime component

The DateTime module differs from Time in that it mainly provides classes for manipulating dates and times.

The datetime module file mainly provides six classes including time and date processing and timezone related classes

The class name describe
datetime.date Used to represent dates. Common attributes are year, month, and day
datetime.time This parameter indicates the time. The common attributes are hour, minute, second, and microsecond
datetime.datetime Used to represent a date or time
datetime.timedelta Indicates the time interval between two instances of Date, time, and datetime in microseconds
datetime.tzinfo Abstract base class for timezone information objects. They are used by the DateTime and time classes to provide custom time adjustments.
datetime.timezone A new feature in Python 3.2 that implements tzinfo abstract base classes representing a fixed offset from UTC

Datetime module related class diagram:

The objects of classes in the đŸ””datetime module are immutable types

Date and time are the two classes we use most in daily life

3. The datetime constants

In the datetime.pyi file, we know that the deteTime module defines two constants

The name of the constant describe
datetime.MINYEAR The minimum number of years allowed by datetime.date or datetime.datetime objects, with a value of 1
datetime.MAXYEAR The maximum number of years allowed by a datetime.date or datetime.datetime object is 9999

4. Common datetime methods

The datetime module provides more operations on dates and times than the time module

methods role
datetime.datetime.timestamp() Converts a Datetime object to output as a timestamp
datetime.datetime.now() Format the current system time
datetime.datetime.timedelta([hours,days]) Sets the offset of a property
datetime.datetime.strptime(date_str,format) Converts a time string to a Datetime object
datetime.date.fromtimestamp(timestamp) Format the timestamp output as a date
datetime.date.today() Returns the local local date
datetime.time.strftime(fromat) Returns local time formatted output
datetime.time.dst() Returns the local time zone, tzinfo none, none
datetime.tzinfo.utcoffset(dt) Set the time zone difference, east to positive, and return the Timedelta object
datetime.tzinfo.dst(dt) Changing daylight saving time returns a Timedelta object

5. Test the cat

The datetime module is used as a datetime module. The datetime module is used as a datetime module. The datetime module is used as a datetime module

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 = ['/'.The '-'.'_']
    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
Copy the code

Let’s test the results:

print("Current date:",datetime.datetime.now())
print("The day before test:",check_current_date("2021-10-29"))
print("Test current date:",check_current_date("2021/10/30"))
Copy the code

conclusion

In this installment, we will take a basic look at the Datetime module. The datetime module contains six classes, and we will learn and practice common methods.

We’ll look at each class of the DateTime module in detail later.

That’s the content of this episode. Please give us your thumbs up and comments. See you next time