python datetime module
Python often used for time modules are time, datetime and calendar, as the name implies, time is to represent time (hours, minutes, seconds, milliseconds), etc., calendar is to represent the calendar time, this chapter discusses the datetime module first.
Let's see what the datetime module has to offer
What's in the datetime module
serial number | descriptive | typology | |
---|---|---|---|
1 | MAXYEAR | 9999 for the maximum number of years that can be supported | int |
2 | MINYEAR | 1, referring to the smallest year that can be supported | int |
3 | date | Classes representing dates, commonly used attributes are year, month, day | type |
4 | datetime | Classes representing date and time, commonly used attributes are hour, minute, second, microsecond | type |
5 | datetime_CAPI | Described in detail later | 'PyCapsule' class |
6 | sys | Various system information | 'module' class |
7 | time | Classes that represent time | type |
8 | timedelta | Indicates a time interval, i.e., the interval between two points in time. Do days, hours, minutes, seconds, milliseconds, subtle time calculations on dates | type |
9 | timezone | Classes representing time zones | type |
10 | tzinfo | Information about time zones | type |
To be precise, the common classes in the datetime module are date, datetime, time, timedelta, timezone, tzinfo.
Three of the classes, date, datetime, and time, have very similar properties and methods.
A demonstration of the creation of the three classes is shown below:
import datetime date_sample=(2021,07,20) # Year, month, day time_sample=(12,20,33) #Hours, minutes, seconds datetime_sample=(2021,7,20,12,20,33)#day of the month, year, hour, minute and second
The python datetime module is highlighted below, as shown below:
briefcase
from datetime import datetime
1. Obtaining the current time
now = () print(now) print(type(now)) print() print() print() print() print() print() print()
2. Time interval timedelta
from datetime import timedelta
Two datetime objects are subtracted to get a timedelta object
# Calculate the time interval delta1 = datetime(2022, 6, 30, 20) - datetime(2022, 2, 2, 1) print(delta1) print(type(delta1))
As shown, it represents 148 days and 19 hours.
print(datetime(2022, 6, 10) + delta1)
timedelta passes in a different number of parameters, the meanings indicated are summarized below.
The first indicates days, the second seconds, and the third microseconds. The fourth represents milliseconds, the fifth represents minutes, and the sixth represents hours.
print(timedelta(10)) print(timedelta(10,11)) print(timedelta(10, 11, 12)) print(timedelta(10, 11, 12, 13)) print(timedelta(10, 11, 12, 13, 14)) print(timedelta(10, 11, 12, 13, 14, 15))
3. datetime to string strftime()
stamp = datetime(2022, 6, 22) # Force conversion of strings print(str(stamp)) # Formatting Converting strings print(("%Y/%m/%d %H:%M:%S")) print(("%Y-%m-%d %H:%M:%S")) print(("%Y/%m/%d")) print(("%Y-%m-%d"))
4. string to datetime object ()
dates = ['1/6/2022', '6/1/2022'] datelist = [(i, "%m/%d/%Y") for i in dates] print(datelist)
5. parse() parse string date
Parses a date in string form into a datetime object.
Strings can be written in many ways, as shown in the following example.
from import parse print(parse('1/6/2022')) print(parse('2022-6-2')) print(parse('2022.6.3')) print(parse('2022 6 4')) print(parse('2022, 6, 5'))
to this article on the python datetime module is introduced to this article, more related python datetime module content, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future!