duc*_*man 5 python datetime python-3.x
我正在读取日期时间格式的字符串。然而,毫秒的位数不是 6 位,而是只有 3 位,末尾带有字母 Z。如何读取该字符串并将其设为日期时间对象,添加 1 天,然后按上述格式将其写为字符串,即 3 位数字表示毫秒,末尾有一个字母 Z。我尝试了以下代码但没有成功:
old_date= "2018-06-06T23:59:59.999Z"
new_date = datetime.datetime.strptime(old_date, '%Y-%m-%d %H:%M:%S.%f%Z') + datetime.timedelta(days=1)
print(new_date)
Run Code Online (Sandbox Code Playgroud)
你应该使用这种格式
datetime.datetime.strptime(old_date, '%Y-%m-%dT%H:%M:%S.%fZ')
Out[180]: datetime.datetime(2018, 6, 6, 23, 59, 59, 999000)
Run Code Online (Sandbox Code Playgroud)
更新
dt1=datetime.datetime.strptime(old_date, '%Y-%m-%dT%H:%M:%S.%fZ')+datetime.timedelta(days=1)
dt1.strftime('%Y-%m-%d %H:%M:%S.%f')[:-3]+'Z'
Out[196]: '2018-06-07 23:59:59.999Z'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6986 次 |
| 最近记录: |