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

前端 未结 6 1968
隐瞒了意图╮
隐瞒了意图╮ 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条回答
  •  南方客
    南方客 (楼主)
    2020-12-23 17:15

    Does anyone else else think it's a waste to convert these strings to date/time objects for what is, in the end, a simple text transformation? If you're certain the incoming dates will be valid, you can just use:

    >>> ddmmyyyy = "21/12/2008"
    >>> yyyymmdd = ddmmyyyy[6:] + "-" + ddmmyyyy[3:5] + "-" + ddmmyyyy[:2]
    >>> yyyymmdd
    '2008-12-21'
    

    This will almost certainly be faster than the conversion to and from a date.

提交回复
热议问题