python datetime.datetime 时间转换
Posted 黑马1723
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python datetime.datetime 时间转换相关的知识,希望对你有一定的参考价值。
1、字符串转换为datetime.datetime:
print(datetime.datetime.strptime("2020-07-09 18:21:17", "%Y-%m-%d %H:%M:%S"))
输出:2020-07-09 18:21:17
2、tuple、list转换为datetime.datetime:
datetime_list = [2020, 7, 10, 9, 40, 47] datetime_tuple = (2020, 7, 10, 9, 40, 47) print(datetime.datetime(*datetime_list)) print(datetime.datetime(*datetime_tuple))
输出:
2020-07-10 09:40:47
2020-07-10 09:40:47
3、日期加减
print(datetime.datetime.now()) print(datetime.datetime.now() + datetime.timedelta(hours=1)) print(datetime.datetime.now() + datetime.timedelta(days=1)) print(datetime.datetime.now() + datetime.timedelta(weeks=1))
输出:
2020-07-14 11:34:47.671556
2020-07-14 12:34:47.671556
2020-07-15 11:34:47.671556
2020-07-21 11:34:47.671556
4、修改时间为指定时间(查询是从00:00:00开始查询)
print((datetime.datetime.now() + datetime.timedelta(days=1)).strftime("%Y-%m-%d 00:00:00"))
输出:
2020-07-15 00:00:00
5、10 && 13位时间戳:
print(int(datetime.datetime.now().timestamp())) print(int(datetime.datetime.now().timestamp())*1000) print(int(datetime.datetime(2020, 7, 10, 9, 40, 47).timestamp())) print(int(datetime.datetime(2020, 7, 10, 9, 40, 47).timestamp())*1000)
输出:
1594698366
1594698366000
1594345247
1594345247000
以上是关于python datetime.datetime 时间转换的主要内容,如果未能解决你的问题,请参考以下文章