简体   繁体   English

如何在 python 中将未格式化的字符串转换为日期和时间

[英]How to convert an unformatted string to date and time in python

Say I have a 10 digit string with the first 6 digits being a date and the last 4 digits being military time.假设我有一个 10 位字符串,前 6 位是日期,后 4 位是军用时间。

1504230001 1504230001

The first 2 digits are year, next 2 are month, next 2 are day.前 2 位是年,后 2 位是月,后 2 位是日。 The remaining 4 digits are for military time其余 4 位数字为军用时间

Desired output:所需的 output:

2015-04-23 00:01:00 2015-04-23 00:01:00

As mentioned in the comments datetime.strptime() is the way to go:正如评论中提到的datetime.strptime()是 go 的方式:

from datetime import datetime

time = 1504230001
time2 = datetime.strptime(str(time), '%y%m%d%H%M')

print(time2.strftime('%Y-%m-%d %H:%M:%S'))

Output: Output:

2015-04-23 00:01:00

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM