Search code examples
pythonpython-3.xutcpython-datetime

How can I convert this date/time to yyyymmdd in Python?


I am given this 1974-11-27T00:00:00 but I don't know what you call this format so I can't find anything online on how to make this yyyymmdd


Solution

  • That's called an ISO formatted date time string. You can turn that into a datetime object by just doing:

    import datetime
    example = '1974-11-27T00:00:00'
    d = datetime.datetime.fromisoformat(example)
    

    Now that's it's a date-time object, you can format it however you want:

    print(d.strftime('%Y%m%d'))
    >> 19741127