One - One Code All

Blog Content

ValueError: 时间数据与格式 '%Y/%m/%d %H:%M:%S' 不匹配

Python   2018-06-21 12:54:26

日期格式转换报错。

import datetime

if __name__ == "__main__":

    d1 = '2021-03-24T11:20:33Z'
    d2 = "2021-03-10T20:45:01.036000Z"
    d = d2 # for quick change

    try:
        next_month = datetime.datetime.strptime(
            d, '%Y-%m-%dT%H:%M:%S.%fZ')
        print('try: ', next_month)
    except ValueError:
        next_month = datetime.datetime.strptime(
            d, '%Y-%m-%dT%H:%M:%SZ')
        print('except: ', next_month)



上一篇:django中json/dict类型数据更新字段
下一篇:python Flask,上传文件,request.files

The minute you think of giving up, think of the reason why you held on so long.