Python 标准库系列之 datetime 模块
Fast implementation of the datetime type.
推荐的日期格式:ISO 8601
yyyy-mm-dd hh:mm:ss 2021-3-19 10:20:08
1
功能 | 说明 |
---|---|
datetime.date.today() | 打印输出当前的系统日期 |
datetime.date.fromtimestamp(time.time()) | 将时间戳转成日期格式 |
datetime.datetime.now() | 打印当前的系统时间 |
current_time.replace(2016,5,12) | 返回当前时间,但指定的值将被替换 |
datetime.datetime.strptime("21/11/06 16:30", "%d/%m/%y %H:%M") | 将字符串转换成日期格式 |
输出当前系统时间
>>> print(datetime.date.today())
2016-05-25
1
2
2
将时间戳格式转换为日期格式
>>> time.time()
# 时间戳格式
1464156222.1887317
>>> print(datetime.date.fromtimestamp(time.time()))
# 日期格式
2016-05-25
1
2
3
4
5
6
2
3
4
5
6
将日期格式转换为 struct_time 格式
>>> current_time = datetime.datetime.now()
>>> print(current_time)
2016-05-25 14:05:26.706667
>>> print(current_time.timetuple())
# 返回struct_time格式
time.struct_time(tm_year=2016, tm_mon=5, tm_mday=25, tm_hour=14, tm_min=5, tm_sec=26, tm_wday=2, tm_yday=146, tm_isdst=-1)
1
2
3
4
5
6
2
3
4
5
6
替换当前系统时间
>>> print(current_time.replace(2016,5,12))
2016-05-12 14:05:26.706667
1
2
2
将字符串转换成日期格式
>>> str_to_date = datetime.datetime.strptime("21/11/06 16:30", "%d/%m/%y %H:%M")
>>> print(str_to_date)
2006-11-21 16:30:00
1
2
3
2
3
时间相加减
比现在加 10 天
>>> new_date = datetime.datetime.now() + datetime.timedelta(days=10)
>>> print(new_date)
2016-06-04 14:10:36.119523
1
2
3
2
3
比现在减 10 天
>>> new_date = datetime.datetime.now() + datetime.timedelta(days=-10)
>>> print(new_date)
2016-05-15 14:11:06.739814
1
2
3
2
3
比现在减 10 小时
>>> new_date = datetime.datetime.now() + datetime.timedelta(hours=-10)
>>> print(new_date)
2016-05-25 04:11:44.095624
1
2
3
2
3
比现在+120s
>>> new_date = datetime.datetime.now() + datetime.timedelta(seconds=120)
>>> print(new_date)
2016-05-25 14:14:02.090219
1
2
3
2
3
借助f-string
格式化当前时间
from datetime import datetime
dtn = datetime.now()
f'{dtn:%F %X}' # '2021-03-10 11:48:20'
1
2
3
2
3
# 推荐阅读
ISO 8601: the better date format | Blog | Kirby Kevinson (opens new window)
编辑 (opens new window)
上次更新: 2024-07-15, 03:27:09