1.OS模块
2.DateTime模块
DateTime模块转换格式表:
3.相关操作
from datetime import datetime, date, time, timedelta
# 自定义时间
d = datetime(2018, 11, 20, 21, 58)
print(d)
d2 = date(2018, 5, 1)
print(d2)
t = time(10, 15)
print(t)
print("-------------------------")
# 日期、时间、字符串之间的相互转化
ds = '2019/7/7 18:26:31'
ds_t = datetime.strptime(ds, '%Y/%m/%d %H:%M:%S')
print(ds_t)
print("-------------------------")
# datetime对象转换成字符串
n = datetime.now()
print(n)
n_str = n.strftime('%Y/%m/%d %H:%M:%S')
print(n_str)
# datetime之间的加减操作:timedelta
print("-------------------------")
# 当前时间
print("当前时间:{}".format(n))
# timedelta操作
n_next = n + timedelta(days=5, hours=42, minutes=42, seconds=56, microseconds=112)
print(n_next)
输出:
2018-11-20 21:58:00
2018-05-01
10:15:00
-------------------------
2019-07-07 18:26:31
-------------------------
2019-04-21 15:22:02.601159
2019/04/21 15:22:02
-------------------------
当前时间:2019-04-21 15:22:02.601159
2019-04-28 10:04:58.601271