Converting date between DD/MM/YYYY and YYYY-MM-DD?

前端 未结 6 1966
隐瞒了意图╮
隐瞒了意图╮ 2020-12-23 16:24

Using a Python script, I need to read a CVS file where dates are formated as DD/MM/YYYY, and convert them to YYYY-MM-DD before saving this into a SQLite database.

Th

6条回答
  •  -上瘾入骨i
    2020-12-23 16:55

    Your example code is wrong. This works:

    import datetime
    
    datetime.datetime.strptime("21/12/2008", "%d/%m/%Y").strftime("%Y-%m-%d")
    

    The call to strptime() parses the first argument according to the format specified in the second, so those two need to match. Then you can call strftime() to format the result into the desired final format.

提交回复
热议问题