1. Date and time objects
- A datetime object is an instance that has both date (year, month, day) and time (hours, minutes, and seconds) attributes
- The datetime object is of type
- Commonly used attributes of a datetime object are year, month, day, hour, minute, second, microsecond
- Date-time objects can be created at a specific time, or by getting the current time.
- The date and time object can be created by positional parameter or keyword parameter when creating the specified time.
- The creation functions for date-time objects are (), (), (), (), ()
- Date and time objects created by () parameters in order: year, month, day, hour, minute, second, microsecond
- Date and time objects are created without parameters by the () function.
- Date and time objects are created without parameters by the () function.
- Date and time objects are created without parameters by the () function.
- The datetime object should contain at least three parameters: year, month, and day when it is created via ().
- The date-time object is created with the following range of parameters via ()
serial number | formal parameter | Real Parameter Scope |
---|---|---|
1 | year | 1~9999 |
2 | month | 1~12 |
3 | day | 0~23 |
4 | hour | 0~23 |
5 | minute | 0~59 |
6 | second | 0~59 |
7 | microsecond | 1~999999 |
2, create date and time objects
2.1. Creation through ()
datetime_zero = ()
2.2. Created by the () function
datetime_first = ()
2.3. Creation through ()
datetime_second = ()
2.4. Creation through ()
- Specify date and time to create
- Must transmit year, month and day parameters
- Specify the date and time, the order of the location parameters is not changeable and the parameter values must be within the specified range.
datetime_three = (year=1, month=1, day=1, hour=0, minute=0, second=0, microsecond=1) datetime_four = (year=9999, month=12, day=31, hour=23, minute=59, second=59, microsecond=999999) datetime_five = (9999, 12, 31, 23, 59, 59, 999999)
2.5 Viewing Created Objects
print(datetime_zero, type(datetime_zero)) # 2022-07-09 18:12:43.486469 <class ''> print(datetime_first, type(datetime_first)) # 2022-07-09 18:12:43.486469 <class ''> print(datetime_second, type(datetime_second)) # 2022-07-09 18:12:43.486469 <class ''> print(datetime_three, type(datetime_three)) # 0001-01-01 00:00:00.000001 <class ''> print(datetime_four, type(datetime_four)) # 9999-12-31 23:59:59.999999 <class ''> print(datetime_five, type(datetime_five)) # 9999-12-31 23:59:59.999999 <class ''>
2.6. View the largest datetime object and the smallest datetime object that datetime can handle.
print() # 0001-01-01 00:00:00 print() # 9999-12-31 23:59:59.999999
3. Properties of the date event object
datetime_first = () """# Get the date attribute [year-month-day] from the datetime object """" new_time = (datetime_first) print(new_time) print(type(new_time)) """# Get the time attribute [hours:minutes:seconds:microseconds] from the datetime object.""" new_time = (datetime_first) print(new_time) print(type(new_time)) """# Get year from datetime object """ datetime_year = datetime_first.year print(datetime_year, type(datetime_year)) # 2022 <class 'int'> """# Get month from datetime object """ datetime_month = datetime_first.month print(datetime_month, type(datetime_month)) # 7 <class 'int'> """# Get days from datetime object """ datetime_day = datetime_first.day print(datetime_day, type(datetime_day)) # 10 <class 'int'> """# Get hour from datetime object """ datetime_hour = datetime_first.hour print(datetime_hour, type(datetime_hour)) # 18 <class 'int'> """# Get minutes from datetime objects """ datetime_minute = datetime_first.minute print(datetime_minute, type(datetime_minute)) # 56 <class 'int'> """# Get seconds from a datetime object.""" datetime_second = datetime_first.second print(datetime_second, type(datetime_second)) # 16 <class 'int'> """# Get microseconds from datetime objects """ datetime_microsecond = datetime_first.microsecond print(datetime_microsecond, type(datetime_microsecond)) # 735264 <class 'int'>
"""# () function's arguments can only be of type """"
date_time = (2022, 12, 26)"""# Incoming parameters cannot be of type """"
“”“# TypeError: descriptor ‘date’ for ‘’ objects doesn’t apply to a ‘’ object”“”
“”“# print((date_time))”“”time_time = (12, 2, 54, 999999)
"""# Incoming parameters cannot be of type """"
“”“# TypeError: descriptor ‘date’ for ‘’ objects doesn’t apply to a ‘’ object”“”
“”“# print((time_time))”“”
"""# Similarly, the arguments passed to the () function cannot be of type and type """"
“”“# TypeError: descriptor ‘time’ for ‘’ objects doesn’t apply to a ‘’ object”“”
“”“# print((date_time))”“”
“”“# TypeError: descriptor ‘time’ for ‘’ objects doesn’t apply to a ‘’ object”“”
“”“# print((time_time))”""
4, date and time objects converted to time tuples
- A time tuple is a tuple object with the year, month, day, hour, minute, second, Nth day of the week, Nth day of the year, and Daylight Saving Time flags.
- Example time tuple: (tm_year=2022, tm_mon=7, tm_mday=9, tm_hour=19, tm_min=14, tm_sec=27, tm_wday=5, tm_yday=190, tm_isdst=0)
- The value of tm_wday starts from 0, the range is: 0~6, 0 is Monday, 6 is Sunday; tm_isdst=0 represents that Daylight Saving Time is not enabled.
UTCDateTime = (year=2022, month=7, day=10, hour=19, minute=14, second=27, microsecond=1235) datetime_UTCTimeTuple = (UTCDateTime) print(datetime_UTCTimeTuple, type(datetime_UTCTimeTuple)) # categorized as:<class 'time.struct_time'>
5, the date and time object will be converted to the AD calendar to start counting the number of days
- Date-time objects are converted to days counted from the beginning of the A.D. calendar
- Converts a plastic value to a datetime object
- The maximum value of the shaping value is 3652059
datetime_replace = (year=2022, month=7, day=9, hour=19, minute=14, second=27, microsecond=123) datetime_ordinal = (datetime_replace) print(datetime_ordinal, type(datetime_ordinal)) # 738345 <class 'int'> print((1)) # 0001-01-02 00:00:00 print((2)) # 0001-01-02 00:00:00 datetime_replace_max = (year=9999, month=12, day=31, hour=23, minute=59, second=59, microsecond=999999) print((datetime_replace_max)) print((3652060))
6, date and time object is converted to a date format value of the string
- For example, Sat Jul 9 19:14:27 2022 (Saturday, July 9, 2022)
- The first part of the value represents the day of the week
- The values in the second part represent months
- Delegate days for part III
- The values in the fourth part represent time
- Values in part V represent years
datetime_replace = (year=2022, month=7, day=9, hour=19, minute=14, second=27, microsecond=123) print(datetime_replace) ctime_datetime = (datetime_replace) print(ctime_datetime, type(ctime_datetime)) ```  ## 7. Converting datetime objects to timestamps ```python datetime_timestamp = (datetime_replace) print(datetime_timestamp, type(datetime_timestamp)) # 1657365267.000123 <class 'float'> ```  ## 8. Converting timestamps to datetime objects ```python print((datetime_timestamp)) # 2022-07-09 19:14:27.000123 ```  ## 9. Date-time objects converted to time tuples ```python datetime_timetuple = (datetime_replace) print(datetime_timetuple, type(datetime_timetuple)) ```  ## 10. ISO standard date-time format ISOStandard calendar time,CalendarCalendar in Chinese * The meaning of each value is(particular year、weeks、The first of the weekNsky)assume (office)(year, week, weekday); * weekdayThe value of the[1,7],1Delegate Monday,7Delegate Sunday * typical example:(year=2022, week=27, weekday=7) ```python datetime_replace = (year=2022, month=7, day=9, hour=19, minute=14, second=27, microsecond=123) UTCDateTime = (year=2022, month=7, day=10, hour=19, minute=14, second=27, microsecond=1235) # ISO standard date-time format print((UTCDateTime)) # Convert datetime objects to strings in ISO standard datetime format UTC_ISO_DateTime = (UTCDateTime) print(UTC_ISO_DateTime, type(UTC_ISO_DateTime)) # 2022-07-10T19:14:27.001235 <class 'str'> # Convert ISO standard datetime format strings to datetime types From_UTC_ISO_DateTime = ('9999-12-31T23:59:59.999999') # <class ''> print(From_UTC_ISO_DateTime, type(From_UTC_ISO_DateTime)) # N day of the week for ISO standards # The range of values is [1,7], 1 for Monday and 7 for Sunday. UTC_ISO_WeekDateTime = (UTCDateTime) print(UTC_ISO_WeekDateTime, type(UTC_ISO_WeekDateTime)) # 7 <class 'int'> # ISO standardized calendar time, Calendar in Chinese. # The meaning of each value is (year, week, weekday) i.e. (year, week, weekday). # weekday value is [1,7], 1 for Monday, 7 for Sunday # Example:(year=2022, week=27, weekday=7) UTC_ISO_CalendarDateTime = (UTCDateTime) print(UTC_ISO_CalendarDateTime, type(UTC_ISO_CalendarDateTime)) # Convert ISO standard calendar format strings to time-date type From_UTC_ISO_CalendarDateTime = (year=2022, week=27, day=7) print(From_UTC_ISO_CalendarDateTime) # 2022-07-10 00:00:00 print(type(From_UTC_ISO_CalendarDateTime)) # <class ''> ```  ## 11, date and time replacement function replace () * replace()You can replace only one of the datetime attributes * replace()The first parameter of the function must be passed * replace()The first argument to the function is a datetime type()target audience * Pass parameter replacement by keyword * parameter shift by position ```python datetime_replace = (year=2022, month=7, day=9, hour=19, minute=14, second=27, microsecond=123) # Initial value print(f"datetime_replaceThe original value of:{datetime_replace}", f"The type is:{type(datetime_replace)}") # No parameters passed print((datetime_replace)) # 2022-07-09 19:14:27.000123 # Replace only the year print((datetime_replace, 2019)) # 2019-07-09 19:14:27.000123 print((datetime_replace, year=2019)) # 2019-07-09 19:14:27.000123 # Replace only the month, same for other parameters. print((datetime_replace, month=12)) # 2022-12-09 19:14:27.000123 print((datetime_replace, datetime_replace.year, 12)) # 2022-12-09 19:14:27.000123 # Replace other parameters with the same print((datetime_replace, year=2019, month=12, day=31, hour=15, minute=13, second=15, microsecond=9999)) # 2019-12-31 15:13:15.009999 ```  ## 12, date time object formatting strftime() * The commonly used formats for formatting date-time objects are as follows * %H(Double-digit hours) * %M(Double-digit minutes) * %S(Two-digit seconds.) * %f(6Microseconds of digits) * %h(Abbreviated month names,Generally English abbreviation) * %y(两位数的particular year) * %Y(四位数的particular year) * %m(Double-digit months) * %d(两位数的sky数) * It is possible to format only some of the attributes ```python datetime_replace = (year=2022, month=7, day=9, hour=19, minute=14, second=27, microsecond=123) # Can format only some of the attributes datetime_str = (datetime_replace, "%Y-%m-%d %H:%M:%S.%f") print(f"Formatted as:{datetime_str}", type(datetime_str)) # 2022-07-09 19:14:27.000123 <class 'str'> # Formatting date attributes datetime_str_date = (datetime_replace, "%Y-%m-%d") print(f"格式化日期The value of the:{datetime_str_date}") # 2022-07-09 # Format time attributes datetime_str_time = (datetime_replace, "%H:%M:%S.%f") print(f"格式化时间The value of the:{datetime_str_time}") # 19:14:27.000123 ```  ## Appendix, full code ```python # coding:utf-8 import datetime # Date-time objects # A date-time object is an instance that has both date (year, month, day) and time (hour, minute, second) attributes # The date-time object is of type # Commonly used properties of the date-time object are year, month, day, hour, minute, second, microsecond, etc. # Date-time objects can be created at a specific time, or by getting the current time. # DateTime objects can be created by positional passes or keyword passes when creating a specified time. # The date-time object creation functions are (), (), (), (), () # The date and time objects are created by () with the following parameters: year, month, day, hour, minute, second, microsecond. # Date-time objects are created without parameters by the () function # Date-time objects are created without parameters by the () function # Date-time objects are created without parameters by the () function # The date-time object should contain at least three parameters, year, month and day, when created via () # The date-time object is created with the following range of parameters via () # {year[1~9999]、month[1~12]、day[1~31]、hour[0~23]、minute[0~59]、second[0~59]、microsecond[1~999999]} # Created by () datetime_zero = () # Created by the () function datetime_first = () # Created by () datetime_second = () # Specify datetime, keyword passes through () function to create datetime_three = (year=1, month=1, day=1, hour=0, minute=0, second=0, microsecond=1) datetime_four = (year=9999, month=12, day=31, hour=23, minute=59, second=59, microsecond=999999) # Specify the date and time through the () function, created by passing the parameters by location, the order is immutable and the parameter values must be within the specified range. datetime_five = (9999, 12, 31, 23, 59, 59, 999999) print(datetime_zero, type(datetime_zero)) # 2022-07-09 18:12:43.486469 <class ''> print(datetime_first, type(datetime_first)) # 2022-07-09 18:12:43.486469 <class ''> print(datetime_second, type(datetime_second)) # 2022-07-09 18:12:43.486469 <class ''> print(datetime_three, type(datetime_three)) # 0001-01-01 00:00:00.000001 <class ''> print(datetime_four, type(datetime_four)) # 9999-12-31 23:59:59.999999 <class ''> print(datetime_five, type(datetime_five)) # 9999-12-31 23:59:59.999999 <class ''> # View the largest and smallest datetime objects that datetime can handle print() # 0001-01-01 00:00:00 print() # 9999-12-31 23:59:59.999999 """# Get the date attribute [year-month-day] from the datetime object """" new_time = (datetime_first) print(new_time) print(type(new_time)) """# Get the time attribute [hours:minutes:seconds:microseconds] from the datetime object.""" new_time = (datetime_first) print(new_time) print(type(new_time)) """# Get year from datetime object """ datetime_year = datetime_four.year print(datetime_year, type(datetime_year)) # 9999 <class 'int'> """# Get month from datetime object """ datetime_month = datetime_four.month print(datetime_month, type(datetime_month)) # 12 <class 'int'> """# Get days from datetime object """ datetime_day = datetime_four.day print(datetime_day, type(datetime_day)) # 31 <class 'int'> """# Get hour from datetime object """ datetime_hour = datetime_four.hour print(datetime_hour, type(datetime_hour)) # 23 <class 'int'> """# Get minutes from datetime objects """ datetime_minute = datetime_four.minute print(datetime_minute, type(datetime_minute)) # 59 <class 'int'> """# Get seconds from a datetime object.""" datetime_second = datetime_four.second print(datetime_second, type(datetime_second)) # 59 <class 'int'> """# Get microseconds from datetime objects """ datetime_microsecond = datetime_four.microsecond print(datetime_microsecond, type(datetime_microsecond)) # 999999 <class 'int'> """# () function's arguments can only be of type """ date_time = (2022, 12, 26) """# Incoming parameters cannot be of type """" """# TypeError: descriptor 'date' for '' objects doesn't apply to a '' object""" """# print((date_time))""" time_time = (12, 2, 54, 999999) """# Incoming parameters cannot be of type """" """# TypeError: descriptor 'date' for '' objects doesn't apply to a '' object""" """# print((time_time))""" """# Similarly, the () function cannot be passed parameters of type and type """" """# TypeError: descriptor 'time' for '' objects doesn't apply to a '' object""" """# print((date_time))""" """# TypeError: descriptor 'time' for '' objects doesn't apply to a '' object""" """# print((time_time))""" # Convert datetime objects to time tuple types # A time tuple is a tuple of objects with the year, month, day, hour, minute, second, Nth day of the week, Nth day of the year, and Daylight Saving Time flags. # Example time tuple: (tm_year=2022, tm_mon=7, tm_mday=9, tm_hour=19, tm_min=14, tm_sec=27, tm_wday=5, tm_yday=190, tm_isdst=0) # where tm_wday value from 0, the range is: 0 ~ 6, 0 for Monday, 6 for Sunday; tm_isdst = 0 on behalf of not enabled daylight saving time UTCDateTime = (year=2022, month=7, day=10, hour=19, minute=14, second=27, microsecond=1235) datetime_UTCTimeTuple = (UTCDateTime) print(datetime_UTCTimeTuple, type(datetime_UTCTimeTuple)) # Type: <class 'time.struct_time' > # Conversion of date-time objects into days counted from the beginning of the AD calendar datetime_replace = (year=2022, month=7, day=9, hour=19, minute=14, second=27, microsecond=123) datetime_ordinal = (datetime_replace) print(datetime_ordinal, type(datetime_ordinal)) # 738345 <class 'int'> print((1)) # 0001-01-02 00:00:00 print((2)) # 0001-01-02 00:00:00 # ctime() is to convert a datetime type to a string of values between dates, e.g. Sat Jul 9 19:14:27 2022 (Saturday, July 9, 2022) # ctime() returns the first part of the value for the day of the week, the second part of the value for the month, the third part of the value for the day, the fourth part of the value for the time, and the fifth part of the value for the year. print(datetime_replace) ctime_datetime = (datetime_replace) print(ctime_datetime, type(ctime_datetime)) # Convert datetime objects to timestamps datetime_timestamp = (datetime_replace) print(datetime_timestamp, type(datetime_timestamp)) # 1657365267.000123 <class 'float'> # Convert timestamps to datetime objects print((datetime_timestamp)) # 2022-07-09 19:14:27.000123 # Convert datetime objects to time tuples datetime_timetuple = (datetime_replace) print(datetime_timetuple, type(datetime_timetuple)) # ISO standard date-time format print((UTCDateTime)) # Convert datetime objects to strings in ISO standard datetime format UTC_ISO_DateTime = (UTCDateTime) print(UTC_ISO_DateTime, type(UTC_ISO_DateTime)) # 2022-07-10T19:14:27.001235 <class 'str'> # Convert ISO standard datetime format strings to datetime types From_UTC_ISO_DateTime = ('9999-12-31T23:59:59.999999') # <class ''> print(From_UTC_ISO_DateTime, type(From_UTC_ISO_DateTime)) # N day of the week for ISO standards # The range of values is [1,7], 1 for Monday and 7 for Sunday. UTC_ISO_WeekDateTime = (UTCDateTime) print(UTC_ISO_WeekDateTime, type(UTC_ISO_WeekDateTime)) # 7 <class 'int'> # ISO standardized calendar time, Calendar in Chinese. # The meaning of each value is (year, week, weekday) i.e. (year, week, weekday). # weekday value is [1,7], 1 for Monday, 7 for Sunday # Example:(year=2022, week=27, weekday=7) UTC_ISO_CalendarDateTime = (UTCDateTime) print(UTC_ISO_CalendarDateTime, type(UTC_ISO_CalendarDateTime)) # Convert ISO standard calendar format strings to time-date type From_UTC_ISO_CalendarDateTime = (year=2022, week=27, day=7) print(From_UTC_ISO_CalendarDateTime) # 2022-07-10 00:00:00 print(type(From_UTC_ISO_CalendarDateTime)) # <class ''> # Date-time replacement function replace() # replace() can replace only one of the datetime attributes # The first argument of the replace() function must be passed. # The first argument to the replace() function is an object of datetime type () # Pass parameter replacement by keyword # Pass the parameter body by position datetime_replace = (year=2022, month=7, day=9, hour=19, minute=14, second=27, microsecond=123) # Initial value print(f"datetime_replaceThe original value of:{datetime_replace}", f"The type is:{type(datetime_replace)}") # No parameters passed print((datetime_replace)) # 2022-07-09 19:14:27.000123 # Replace only the year print((datetime_replace, 2019)) # 2019-07-09 19:14:27.000123 print((datetime_replace, year=2019)) # 2019-07-09 19:14:27.000123 # Replace only the month, same for other parameters. print((datetime_replace, month=12)) # 2022-12-09 19:14:27.000123 print((datetime_replace, datetime_replace.year, 12)) # 2022-12-09 19:14:27.000123 # Replace other parameters with the same print((datetime_replace, year=2019, month=12, day=31, hour=15, minute=13, second=15, microsecond=9999)) # 2019-12-31 15:13:15.00999 # date time object formatting strftime() # The following formats are commonly used for date-time object formatting: "" %H(Double-digit hours)、%M(Double-digit minutes)、%S(Two-digit seconds.)、%f(6Microseconds of digits)、%h(Abbreviated month names,Generally English abbreviation) %y(两位数的particular year)、%Y(四位数的particular year)、%m(Double-digit months)、%d(两位数的sky数) """ # Can format only some of the attributes datetime_str = (datetime_replace, "%Y-%m-%d %H:%M:%S.%f") print(f"Formatted as:{datetime_str}", type(datetime_str)) # 2022-07-09 19:14:27.000123 <class 'str'> # Formatting date attributes datetime_str_date = (datetime_replace, "%Y-%m-%d") print(f"格式化日期The value of the:{datetime_str_date}") # 2022-07-09 # Format time attributes datetime_str_time = (datetime_replace, "%H:%M:%S.%f") print(f"格式化时间The value of the:{datetime_str_time}") # 19:14:27.000123 ```
to this article on the Python standard library datetime datetime module usage analysis of the article is introduced to this, more related Python datetime module content, please search for my previous posts or continue to browse the following related articles I hope you will support me in the future!