21 Python’s datetime module

Overview

        In the previous section, we introduced Python's time module, including: some commonly used attributes and functions in the time module. In this section, we will introduce Python’s datetime module. The datetime module is a built-in module of Python that provides a convenient way to handle dates and times. This module contains many classes, including: date, time, datetime, timedelta, etc., used to process date, time, time difference information, etc.

        Below, we will introduce some commonly used functions and classes in the datetime module one by one.

datetime.date class

        datetime.date(year, month, day) is a class representing date, including year, month, day and other information. It accepts three parameters: year, month and day, which are used to specify a specific date.

import datetime

d = datetime.date(2023, 9, 18)
# 输出:2023-09-18
print(d)
# 输出:2023 9 18
print(d.year, d.month, d.day)

datetime.time class

        datetime.time(hour, minute, second, microsecond) is a class that represents time, including information such as hours, minutes, seconds, and microseconds. It accepts four parameters: hour, minute, second and microsecond, which are used to specify a specific time.

import datetime  

t = datetime.time(9, 18, 9, 18)
# 输出:09:18:09.000018
print(t)
# 输出:9 18 9 18
print(t.hour, t.minute, t.second, t.microsecond)

datetime.datetimeClass

        datetime.datetime(year, month, day, hour, minute, second, microsecond) is a class that represents date and time, and is a combination of date and time classes. It accepts seven parameters: year (year), month (month), date (day), hour (hour), minute (minute), second (second) and microsecond (microsecond), used to specify a specific date and time.

import datetime  

dt = datetime.datetime(2023, 9, 18, 9, 18, 9, 18)
# 输出:2023-09-18 09:18:09.000018
print(dt)
# 输出:2023 9 18
print(dt.year, dt.month, dt.day)
# 输出:9 18 9 18
print(dt.hour, dt.minute, dt.second, dt.microsecond)

datetime.timedelta class

        datetime.timedelta(days, seconds, microseconds) is a class that represents the time difference, including information such as days, seconds, and microseconds. It accepts three parameters: days, seconds and microseconds, which are used to specify the size of the time difference.

import datetime

td = datetime.timedelta(days = 1, seconds = 100)
# 输出:1 day, 0:01:40
print(td)
# 输出:1 100 0
print(td.days, td.seconds, td.microseconds)
# 输出:86500.0
print(td.total_seconds())

datetime.tzinfo class

        datetime.tzinfo is a class that represents time zone information and is used to handle time zone related operations. This class is an abstract class, and commonly used subclasses include: timezone, pytz, etc. When creating a subclass from this class, you must rewrite the three functions name(), utcoffset(), and dst(). The datetime.tzinfo class is rarely used in daily work, so it will not be introduced in detail here.

datetime.now() function

        The datetime.now() function is used to get the current date and time.

import datetime

# 输出:2023-09-17 20:29:10.606024
print(datetime.datetime.now())

datetime.strptime() function

        The datetime.strptime(date_string, format) function is used to parse a string into a date and time. Among them, date_string is the string to be parsed, format is the format of the string, and the return value is a datetime object that contains the date and time information parsed from the string.

import datetime

date_str = "2023-09-18 09:18:00"
date_format = "%Y-%m-%d %H:%M:%S"
date_obj = datetime.datetime.strptime(date_str, date_format)
# 输出:2023-09-18 09:18:00
print(date_obj)

Guess you like

Origin blog.csdn.net/hope_wisdom/article/details/133252273