第 16 天
Python日期时间
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 开始经过的秒数。
使用strftime格式化日期输出
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
# current date and time
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 format
print("time one:", time_one)
time_two = now.strftime("%d/%m/%Y, %H:%M:%S")
# dd/mm/YY H:M:S format
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符号。此模块的所有格式的示例。
使用strptime对字符串计时
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:00
使用日期时间中的日期
from datetime import date
d = date(2020, 1, 1)
print(d)
print('Current date:', d.today()) # 2019-12-05
# date object of today's date
today = date.today()
print("Current year:", today.year) # 2019
print("Current month:", today.month) # 12
print("Current day:", today.day) # 5
表示时间的时间对象
from 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
# Time left for new year: 27 days, 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) # Time left for new year: 26 days, 23: 01: 00
使用timedelata的两个时间点之间的差异
from 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 步之遥。现在为你的大脑和肌肉做一些练习。
练习:第 16 天
- 从 datetime 模块获取当前日、月、年、小时、分钟和时间戳
- 使用以下格式格式化当前日期:“%m/%d/%Y, %H:%M:%S”)
- 今天是 2019 年 12 月 5 日。将此时间字符串更改为时间。
- 计算现在和新年之间的时差。
- 计算 1970 年 1 月 1 日与现在的时差。
- 想一想,您可以使用 datetime 模块做什么?例子:
- 时间序列分析
- 获取应用程序中任何活动的时间戳
- 在博客上添加帖子
🎉恭喜!🎉