preamble
datetime is a standard library provided by Python to manipulate date and time, mainly modules, modules and modules. Among them, date module provides date operation related methods; time module provides time operation related methods; datetime provides date time operation related content. This article mainly introduces the use of commonly used functions in the module details.
Date object class:
- The Date object has 3 properties:Year, month, day
- Date objects are created using the date() definition.
- date() can convert incoming arguments to type
- date() function by position must be passed: year, month, day
- date () function of the parameters of the year in the range of 1-9999, the month of the range of 1-12, the day of the range of 1-31
1. Definitions
- Import the datetime library before manipulating
# coding:utf-8 import datetime now_date = (2022, 12, 26) print(now_date)
1.2. Common errors
- Missing parameters
- Parameter value out of range when passing parameter by position
# coding:utf-8 import datetime print((2022, 12, 26)) print(type((2022, 12, 26))) # <class ''> # The following code reports an error with a missing parameter # print((2022, 12)) # TypeError: function missing required argument 'day' (pos 3) # The following code suggests that the value is wrong, the date() function's parameters are, in order, year, month and day; the range of year is 1-9999, the range of month is 1-12, and the range of day is 1-31. # print((10001, 12, 12)) # ValueError: year 10001 is out of range # print((2022, 13, 12)) # ValueError: month must be in 1..12 # print((2022, 12, 32)) # ValueError: day is out of range for month # keyword pass pass parameter, as long as you ensure that the value of the year, month, day are within the available range, the position is not affected print((day=15, year=2029, month=12))
2, date class commonly used functions
2.1. Obtaining the current date
# coding:utf-8 import datetime print(()) # 2022-07-08 print(type(())) # <class ''>
2.2. Formatting dates
2.2.1、ctime()
- Converting an object to a string in datetime format
- The arguments to the ctime() function must be of type
print((())) # Fri Jul 8 00:00:00 2022 print(type((()))) # <class 'str'> # ctime() function's arguments must be Type print(((2022, 12, 26))) # Mon Dec 26 00:00:00 2022
2.2.2, Objects
datetime_1 = (2022, 12, 26) # Get the day of the date object, the value returned is of type int. print(datetime_1.day, type(datetime_1.day), type(datetime_1)) # 26 <class 'int'> <class ''> # Get the month of the date object, the value returned is also of type int. print(datetime_1.month, type(datetime_1.month), type(datetime_1)) # 12 <class 'int'> <class ''> # Get the year of the date object, the value returned is also of type int. print(datetime_1.year, type(datetime_1.year), type(datetime_1)) # 2022 <class 'int'> <class ''>
2.2.3、replace(self, year=None, month=None, day=None)
- Replacing the value of an object
- The replace() function has one mandatory parameter and three default parameters.
- year is the year to be replaced
- month is the month to be replaced
- day is the day to be replaced
datetime_1 = (2022, 12, 26) print((datetime_1, year=2019, month=11, day=25)) # 2019-11-25 print(type((datetime_1, year=2019, month=11, day=25))) # <class ''> print(datetime_1) # Must have a date or time object parameter, not pass a replacement parameter that returns the value of the incoming object. print((datetime_1)) # 2022-12-26 # Replacement year only print((datetime_1, year=2019)) # 2019-12-26 # Replace only the month print((datetime_1, month=11)) # 2022-11-26 # Replacement day only print((datetime_1, day=25)) # 2022-12-25
By observation, the replacement of year, month, and day is based on 2022-12-26 (the initial value of the datetime_1 variable). So it is certain that this function does not change the value of the original variable.
2.2.4. Formatting dates
format symbol |
Meaning of symbols |
%y |
Two-digit year indication (00-99) |
%Y |
Four-digit year indication (000-9999) |
%m |
Month (01-12) |
%d |
Day of the month (0-31) |
%H |
Hours in 24-hour format (0-23) |
%I |
12-hour hours (01-12) |
%M |
Minutes (00-59) |
%S |
Seconds (00-59) |
%a |
Local Simplified Name of the Week |
%A |
Local full week name |
%b |
Local simplified name of the month |
%B |
Local full month name |
%c |
Local corresponding date representation and time representation |
%j |
Day of the year (001-366) |
%p |
Local. or . Equivalents of |
%U |
Number of weeks in a year (00-53) Sunday is the start of the week |
%w |
Day of week (0-6), Sunday is 0, Monday is 1, and so on. |
%W |
Number of weeks in a year (00-53) Monday is the start of the week |
%x |
Local corresponding date representation |
%X |
Local corresponding time representation |
%Z |
Name of the current time zone |
%% |
The % itself |
datetime_2 = () print(datetime_2, type(datetime_2)) print((datetime_1, "%Y--%y--%D--%d--%H--%h--%M--%m--%S--%A--%a--%B--%b--%C--%c")) print((datetime_2, "%Y--%y--%D--%d--%H--%h--%M--%m--%S--%A--%a--%B--%b--%C--%c")) # Common formats: year, month, day, hour, minute, second print((datetime_2, "%Y-%m-%d %H:%M:%S")) # 2022-07-08 18:32:40
2.3. ISO standard format dates
- Format.
(4-digit year - 2-digit month - 2-digit day)
, e.g.(2022,07,08)
# Returns the ISO standard date of a date or time object (4-digit year - 2-digit month - 2-digit day) print((datetime_1), datetime_1) # 2022-12-26 2022-12-26 print((datetime_2), datetime_2) # 2022-07-08 2022-07-08 18:44:36.613676 # Convert date strings conforming to ISO standard format to objects print(("2022-07-08"), type(("2022-07-08"))) # Errors are reported for date strings that do not conform to the ISO standard format. # print(("2022-7-08")) # ValueError: Invalid isoformat string: '2022-7-08' # print(("2022-07-8")) # ValueError: Invalid isoformat string: '2022-7-08'
2.3.1. Get the day of the week (1~7) of a date string that conforms to the ISO standard format.
print(((2022, 7, 3))) # 7 Sunday print(((2022, 7, 4))) # 1 Monday print(((2022, 7, 8))) # 5 Friday print(((2022, 7, 9))) # 6 Saturdays
2.3.2, return to the date or time object of the day of the week (0 ~ 6)
# Returns the day of the week (0~6) for a date or time object. print(((2022, 7, 3))) # 6 Sunday print(((2022, 7, 4))) # 0 Monday print(((2022, 7, 8))) # 4 Friday print(((2022, 7, 9))) # 5 Saturdays
2.3.3 Calculation of dates based on timestamps
print((2015236523)) # 2033-11-10 print(type((2015236523))) # <class ''>
2.3.4 Time tuples in ISO standard format
- Format .
(Year, Week, Weeks)
, e.g.(2022,26,7)
- Weeks: Weeks of the year, ranging from 1 to 53.
- Number of days of the week: the first day of the week, Monday is the first day, Sunday is the seventh day.
# Get time tuples in ISO-compliant format print(((2022, 7, 3))) # (year=2022, week=26, weekday=7) # Calculate dates from time tuples (year, week, week number) in ISO standardized format. print((2022, 26, 7)) # 2022-07-03
2.4. Calendar format dates
- January 1, A.D. 1 for 1
print((1), type((1))) # 0001-01-01 <class ''> print((5)) # 0001-01-05 print((738339)) # 2022-07-03 # Returns the number of days from the beginning of the Gregorian calendar to the present day print(((2022, 7, 3))) # 738339
3. Other
# Convert date types to struct_time types print(((2022, 7, 3))) # Maximum value for date type print() # Minimum values for date types print()
Appendix: Complete Code
# coding:utf-8 import datetime # Date object class (date class) """ * Date objects have3particular property:particular year、months、date * date()It is possible to convert the specified three parameters to the type of the class * date()Functions passing parameters by position must be:particular year、months、date的顺序 """ print((2022, 12, 26)) print(type((2022, 12, 26))) # <class ''> # The following code reports an error with a missing parameter # print((2022, 12)) # TypeError: function missing required argument 'day' (pos 3) # The following code suggests that the value is wrong, the date() function's parameters are, in order, year, month and day; the range of year is 1-9999, the range of month is 1-12, and the range of day is 1-31. # print((10001, 12, 12)) # ValueError: year 10001 is out of range # print((2022, 13, 12)) # ValueError: month must be in 1..12 # print((2022, 12, 32)) # ValueError: day is out of range for month # keyword pass pass parameter, as long as you ensure that the value of the year, month, day are within the available range, the position is not affected print((day=15, year=2029, month=12)) # Common functions of the date class # Get current date print(()) # 2022-07-08 print(type(())) # <class ''> # ctime() is to convert an object to a datetime format string print((())) # Fri Jul 8 00:00:00 2022 print(type((()))) # <class 'str'> # ctime() function's arguments must be Type print(((2022, 12, 26))) # Mon Dec 26 00:00:00 2022 # Create an object datetime_1 = (2022, 12, 26) # Get the day of the date object, the value returned is of type int. print(datetime_1.day, type(datetime_1.day), type(datetime_1)) # 26 <class 'int'> <class ''> # Get the month of the date object, the value returned is also of type int. print(datetime_1.month, type(datetime_1.month), type(datetime_1)) # 12 <class 'int'> <class ''> # Get the year of the date object, the value returned is also of type int. print(datetime_1.year, type(datetime_1.year), type(datetime_1)) # 2022 <class 'int'> <class ''> # Replace the value of the object # replace(self, year=None, month=None, day=None) function has one mandatory argument and three default arguments # year is the year to be replaced # month is the month to be replaced # day is the day to be replaced print((datetime_1, year=2019, month=11, day=25)) # 2019-11-25 print(type((datetime_1, year=2019, month=11, day=25))) # <class ''> print(datetime_1) # Must have a date or time object parameter, not pass a replacement parameter that returns the value of the incoming object. print((datetime_1)) # 2022-12-26 # Replacement year only print((datetime_1, year=2019)) # 2019-12-26 # Replace only the month print((datetime_1, month=11)) # 2022-11-26 # Replacement day only print((datetime_1, day=25)) # 2022-12-25 # By observation, the replacement of year, month, and day are all based on 2022-12-26 (the initial value of the datetime_1 variable). So it is safe to assume that this function does not change the value of the source variable. # Formatted date datetime_2 = () print(datetime_2, type(datetime_2)) print((datetime_1, "%Y--%y--%D--%d--%H--%h--%M--%m--%S--%A--%a--%B--%b--%C--%c")) print((datetime_2, "%Y--%y--%D--%d--%H--%h--%M--%m--%S--%A--%a--%B--%b--%C--%c")) # Common formats: year, month, day, hour, minute, second print((datetime_2, "%Y-%m-%d %H:%M:%S")) # 2022-07-08 18:32:40 # Returns the ISO standard date of a date or time object (4-digit year - 2-digit month - 2-digit day) print((datetime_1), datetime_1) # 2022-12-26 2022-12-26 print((datetime_2), datetime_2) # 2022-07-08 2022-07-08 18:44:36.613676 # Convert date strings conforming to ISO standard format to objects print(("2022-07-08"), type(("2022-07-08"))) # Errors are reported for date strings that do not conform to the ISO standard format. # print(("2022-7-08")) # ValueError: Invalid isoformat string: '2022-7-08' # print(("2022-07-8")) # ValueError: Invalid isoformat string: '2022-7-08' # Get the day of the week (1~7) for a date string that conforms to the ISO standard format. print(((2022, 7, 3))) # 7 Sunday print(((2022, 7, 4))) # 1 Monday print(((2022, 7, 8))) # 5 Friday print(((2022, 7, 9))) # 6 Saturday # Returns the day of the week (0~6) for a date or time object. print(((2022, 7, 3))) # 6 Sunday print(((2022, 7, 4))) # 0 Monday print(((2022, 7, 8))) # 4 Friday print(((2022, 7, 9))) # 5 Saturday # Calculate dates based on timestamps print((2015236523)) # 2033-11-10 print(type((2015236523))) # <class ''> # Get a time tuple in ISO standardized format (year, week, week number) # Weeks: weeks of the year, range 1-53 # of weeks: the first day of the week, Monday is day 1, Sunday is day 7 print(((2022, 7, 3))) # (year=2022, week=26, weekday=7) # Calculate dates from time tuples (year, week, week number) in ISO standardized format. print((2022, 26, 7)) # 2022 -07-03 # Convert shaping to date by A.D. calendar # * January 1, A.D. 1 for 1 print((1), type((1))) # 0001-01-01 <class ''> print((5)) # 0001-01-05 print((738339)) # 2022-07-03 # Returns the number of days from the beginning of the Gregorian calendar to the present day print(((2022, 7, 3))) # 738339 # Convert date types to struct_time types print(((2022, 7, 3))) # Maximum value for date type print() # Minimum values for date types print()
to this article on the Python standard library datetime date module details of the article is introduced to this, more related Python date module content please search for my previous articles or continue to browse the following related articles I hope that you will support me in the future!