[python] datetime from entry to actual combat to master the first step of time operation function

datetime

The datetime library is a standard library for dealing with dates and times in Python. It provides some classes and functions for various operations on dates and times. The following is a summary of the knowledge points of the datetime library:

  1. datetime class

The datetime class is one of the most important classes in the datetime library, which represents a combination of date and time. The constructor of the datetime class is as follows:

datetime(year, month, day[, hour[, minute[, second[, microsecond[, tzinfo]]]]])

Among them, year, month, and day are required parameters, indicating year, month, and day; hour, minute, second, and microsecond are optional parameters, indicating hour, minute, second, and microsecond; tzinfo is time zone information, which is also optional parameters.

Here is an example using the datetime class:

import datetime

# 创建一个datetime对象
dt = datetime.datetime(2021, 7, 1, 12, 30, 0)

# 输出datetime对象的各个属性
print(dt.year)
print(dt.month)
print(dt.day)
print(dt.hour)
print(dt.minute)
print(dt.second)
print(dt.microsecond)

The output is:

2021
7
1
12
30
0
0
  1. date class

The date class represents a date, which is a subclass of the datetime class. The constructor of the date class is as follows:

date(year, month, day)

Among them, year, month, and day represent the year, month, and day, respectively. Here is an example using the date class:

import datetime

# 创建一个date对象
d = datetime.date(2021, 7, 1)

# 输出date对象的各个属性
print(d.year)
print(d.month)
print(d.day)

The output is:

2021
7
1
  1. time class

The time class represents a time, which is a subclass of the datetime class. The constructor of the time class is as follows:

time([hour[, minute[, second[, microsecond[, tzinfo]]]]])

Among them, hour, minute, second, microsecond, and tzinfo represent hour, minute, second, microsecond, and time zone information respectively. They are all optional parameters. Here is an example using the time class:

import datetime

# 创建一个time对象
t = datetime.time(12, 30, 0)

# 输出time对象的各个属性
print(t.hour)
print(t.minute)
print(t.second)
print(t.microsecond)

The output is:

12
30
0
0
  1. timedelta class

The timedelta class represents the time difference between two dates or times. The constructor of the timedelta class is as follows:

timedelta([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, weeks]]]]]]])

Among them, days, seconds, microseconds, milliseconds, minutes, hours, and weeks represent days, seconds, microseconds, milliseconds, minutes, hours, and weeks, respectively. They are all optional parameters. Here is an example using the timedelta class:

import datetime

# 创建两个datetime对象
dt1 = datetime.datetime(2021, 7, 1, 12, 30, 0)
dt2 = datetime.datetime(2021, 7, 2, 13, 30, 0)

# 计算时间差
delta = dt2 - dt1

# 输出时间差的各个属性
print(delta.days)
print(delta.seconds)
print(delta.microseconds)

The output is:

1
3600
0
  1. Constants in the datetime module

The datetime module also defines some constants to represent some special dates and times, for example:

  • datetime.MINYEAR: Indicates the minimum value of the year attribute in the datetime class, and its value is 1;
  • datetime.MAXYEAR: Indicates the maximum value of the year attribute in the datetime class, and its value is 9999;
  • datetime.date.today(): returns the current date;
  • datetime.datetime.now(): returns the current date and time;
  • datetime.datetime.utcnow(): returns the current UTC time.

Here is an example using constants from the datetime module:

import datetime

# 输出datetime类中year属性的最小值和最大值
print(datetime.MINYEAR)
print(datetime.MAXYEAR)

# 输出当前日期和时间
print(datetime.datetime.now())

# 输出当前UTC时间
print(datetime.datetime.utcnow())

The output is:

1
9999
2021-07-01 13:30:00.000000
2021-07-01 05:30:00.000000

Summarize:

The datetime library is a standard library for dealing with dates and times in Python. It provides some classes and functions for various operations on dates and times. Among them, datetime class, date class, time class and timedelta class are the most commonly used classes. The datetime module also defines some constants to represent some special dates and times. Master the use of datetime library, you can easily deal with date and time related issues.

Guess you like

Origin blog.csdn.net/qq_41604569/article/details/131291959