1 Introduction
Several classes defined in the datetime library:
class name | describe | common attributes |
---|---|---|
datetime.date | Indicates the date | year, month, day |
datetime.time | Indicate time | hour, minute, second, microsecond |
datetime.datetime | Indicates date and time | year, month, day, hour, minute, microsecond |
datetime.timedelta | Represents the difference between two datetimes | days, hours, seconds |
datetime.tzinfo | Abstract base class for describing time zone information objects | search online |
datetime.timezone | Timezone, representing a fixed offset from UTC | datetime.timedelta object |
For detailed methods and summary, refer to this article " datetime time and date module "
2. Format the output string
The output string can be formatted as desired: datetime.datetime.strftime("format")
Code example:
import datetime
t = datetime.datetime.now()
print(t.strftime("%Y%m%d"),type(t.strftime("%Y%m%d")))
print(t.strftime("%Y-%m_%d:%H-%M:%S"))
# 输出
# 20220909 <class 'str'>
# 2022-09_09:10-41:33
instruction | significance | example |
%a | abbreviation for local weekday | Sun, Mon, …, Sat |
%A | Full name of the local weekday | Sunday, Monday, …, Saturday |
%w | Weekday as a decimal number, where 0 means Sunday and 6 means Saturday | 0, 1, …, 6 |
%d | The day of the month as a decimal number after zero padding | 01, 02, …, 31 |
%b | local month abbreviation | an, Feb, …, Dec |
%B | full name of the local month | January, February, …, December |
%m | After zero padding, the month displayed as a decimal number | 01, 02, …, 12 |
%y | Year without century in decimal number after padding with zeros | 00, 01, …, 99 |
%Y | Year with century as a decimal number | 0001, 0002, …, 2013, 2014, …, 9998, 9999 |
%H | Hour (24-hour clock) | 00, 01, …, 23 |
%I | Hour (12-hour clock) | 01, 02, …, 12 |
%M | Minutes as a decimal number after zero padding | 00, 01, …, 59 |
%S | Seconds displayed as a decimal number after zero padding | 00, 01, …, 59 |
%U | Week number of the year (Sunday as the first day of the week) | 00, 01, …, 53 |
%W | Week number of the year (Monday as the first day of the week) | 00, 01, …, 53 |
%c | localized appropriate date and time representation | Tue Aug 16 21:30:00 1988 |
%x | localized appropriate date representation | 08/16/1988 |
%X | Appropriate time representation for localization | 0.895833333 |
%% | Literal '%' characters | Literal '%' characters |
3. Commonly used codes
1) Calculate the time difference:
import datetime
# 2022-04-24 10:51:23.677632
start_time = datetime.datetime(2022, 4, 24, 10, 51, 23, 677632)
# 2022-04-25 11:52:35.713161
end_time = datetime.datetime(2022, 4, 24, 10, 51, 24, 687638)
delta = end_time-start_time
# 获取timedelta对象经过单位换算后的总秒数
print("seconds: ", delta.seconds)
# 获取timedelta对象经过单位换算后的总微秒数
print("microseconds: ", delta.microseconds)
# 获取timedelta对象包含的总秒数
print("total_seconds: ", delta.total_seconds())
# 四舍五入,保留2位小数
print("total_seconds: ", round(delta.total_seconds(),2))
# 输出
# seconds: 1
# seconds: 10006
# total_seconds: 1.010006
# total_seconds: 1.01
Example of test timing:
import datetime
import time
start_time = datetime.datetime.now()
print("start_time:",start_time)
# 强制等待10秒时间,import time
time.sleep(10)
end_time = datetime.datetime.now()
print("end_time:",end_time)
delta = end_time-start_time
# 获取timedelta对象经过单位换算后的总秒数
print("seconds: ", delta.seconds)
# 获取timedelta对象经过单位换算后的总微秒数
print("microseconds: ", delta.microseconds)
# 获取timedelta对象包含的总秒数
print("total_seconds: ", delta.total_seconds())
# 四舍五入,保留2位小数
print("total_seconds: ", round(delta.total_seconds(),2))
# 输出
start_time: 2022-09-09 10:54:49.917227
end_time: 2022-09-09 10:54:59.917423
seconds: 10
microseconds: 196
total_seconds: 10.000196
total_seconds: 10.0