Python 有一个 datetime 模块来处理日期和时间。
import datetime
print(dir(datetime))
['MAXYEAR', 'MINYEAR', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'date', 'datetime', 'datetime_CAPI', 'sys', 'time', 'timedelta', 'timezone', 'tzinfo']使用 dir 或 help 内置命令可以知道某个模块中可用的函数。如你所见,datetime 模块中有很多函数,但我们将专注于 date, datetime, time 和 timedelta。让我们一一看一下它们。
from datetime import datetime
now = datetime.now()
print(now) # 2021-07-08 07:34:46.549883
day = now.day # 8
month = now.month # 7
year = now.year # 2021
hour = now.hour # 7
minute = now.minute # 38
second = now.second
timestamp = now.timestamp()
print(day, month, year, hour, minute)
print('timestamp', timestamp)
print(f'{day}/{month}/{year}, {hour}:{minute}') # 8/7/2021, 7:38时间戳或 Unix 时间戳是从 1970 年 1 月 1 日 UTC 起经过的秒数。
from datetime import datetime
new_year = datetime(2020, 1, 1)
print(new_year) # 2020-01-01 00:00:00
day = new_year.day
month = new_year.month
year = new_year.year
hour = new_year.hour
minute = new_year.minute
second = new_year.second
print(day, month, year, hour, minute) #1 1 2020 0 0
print(f'{day}/{month}/{year}, {hour}:{minute}') # 1/1/2020, 0:0使用 strftime 方法格式化日期时间,可以在这里找到文档。
from datetime import datetime
# 当前日期和时间
now = datetime.now()
t = now.strftime("%H:%M:%S")
print("time:", t)
time_one = now.strftime("%m/%d/%Y, %H:%M:%S")
# mm/dd/YY H:M:S 格式
print("time one:", time_one)
time_two = now.strftime("%d/%m/%Y, %H:%M:%S")
# dd/mm/YY H:M:S 格式
print("time two:", time_two)time: 01:05:01
time one: 12/05/2019, 01:05:01
time two: 05/12/2019, 01:05:01下面是我们用来格式化时间的 strftime 符号。此模块的所有格式示例。
这里有一个帮助理解格式的文档。
from datetime import datetime
date_string = "5 December, 2019"
print("date_string =", date_string)
date_object = datetime.strptime(date_string, "%d %B, %Y")
print("date_object =", date_object)date_string = 5 December, 2019
date_object = 2019-12-05 00:00:00from datetime import date
d = date(2020, 1, 1)
print(d)
print('Current date:', d.today()) # 2019-12-05
# 今日日期对象
today = date.today()
print("Current year:", today.year) # 2019
print("Current month:", today.month) # 12
print("Current day:", today.day) # 5from datetime import time
# time(hour = 0, minute = 0, second = 0)
a = time()
print("a =", a)
# time(hour, minute and second)
b = time(10, 30, 50)
print("b =", b)
# time(hour, minute and second)
c = time(hour=10, minute=30, second=50)
print("c =", c)
# time(hour, minute, second, microsecond)
d = time(10, 30, 50, 200555)
print("d =", d)输出结果
a = 00:00:00
b = 10:30:50
c = 10:30:50
d = 10:30:50.200555
today = date(year=2019, month=12, day=5)
new_year = date(year=2020, month=1, day=1)
time_left_for_newyear = new_year - today
# 距离新年的时间:27天,0:00:00
print('Time left for new year: ', time_left_for_newyear)
t1 = datetime(year = 2019, month = 12, day = 5, hour = 0, minute = 59, second = 0)
t2 = datetime(year = 2020, month = 1, day = 1, hour = 0, minute = 0, second = 0)
diff = t2 - t1
print('Time left for new year:', diff) # 距离新年:26天,23:01:00from datetime import timedelta
t1 = timedelta(weeks=12, days=10, hours=4, seconds=20)
t2 = timedelta(days=7, hours=5, minutes=3, seconds=30)
t3 = t1 - t2
print("t3 =", t3) date_string = 5 December, 2019
date_object = 2019-12-05 00:00:00
t3 = 86 days, 22:56:50🌕 你是非凡的。你已经向着伟大的道路迈出了第 16 步。现在为你的大脑和肌肉做一些练习吧。
- 获取当前日期、月、年、小时、分钟和时间戳
- 使用此格式格式化当前日期:“%m/%d/%Y, %H:%M:%S”
- 今天是 2019 年 12 月 5 日。将这个时间字符串转换为时间。
- 计算现在和新年之间的时间差。
- 计算 1970 年 1 月 1 日和现在之间的时间差。
- 想想,你可以使用 datetime 模块做什么?示例:
- 时间序列分析
- 获取应用程序中任何活动的时间戳
- 添加博客上的帖子
🎉 恭喜你 ! 🎉

