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

前端 未结 6 1973
隐瞒了意图╮
隐瞒了意图╮ 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:01

    I am new to programming. I wanted to convert from yyyy-mm-dd to dd/mm/yyyy to print out a date in the format that people in my part of the world use and recognise.

    The accepted answer above got me on the right track.

    The answer I ended up with to my problem is:

    import datetime
    
    today_date = datetime.date.today()
    print(today_date)
    
    new_today_date = today_date.strftime("%d/%m/%Y")
    print (new_today_date)
    

    The first two lines after the import statement gives today's date in the USA format (2017-01-26). The last two lines convert this to the format recognised in the UK and other countries (26/01/2017).

    You can shorten this code, but I left it as is because it is helpful to me as a beginner. I hope this helps other beginner programmers starting out!

提交回复
热议问题