0

I'm having some troubles with Datetime formats. My database comes with this datetime format:

"2020-11-01T03:17:54Z"

Thing is that I just can't find the format that fits this string in order for me to convert it to a datetime object. I've gone all over the documentation but I just can´t find how to write the "T" or what the "Z" means.

Thanks in advance!

  • You'll find the format as ISO 8601 / RFC 3339. For parsing to datetime in Python, see e.g. https://stackoverflow.com/a/62769371/10197418 - or in pandas just use pd.to_datetime and auto-detect the format. – FObersteiner Dec 02 '20 at 19:08

1 Answers1

0

The date is in ISO 8601 format. If you try:

    mydate = "2020-11-01T03:17:54Z"
    mydate_updated = pd.to_datetime(mydate)

you will see that this format includes the info "tz='UTC'":

    Timestamp('2020-11-01 03:17:54+0000', tz='UTC')

You can remove it (but keep that the date is in UTC) as follows:

    mydate_updated = pd.to_datetime(mydate).tz_localize(None)

The result will be the timestamp:

   Timestamp('2020-11-01 03:17:54')
sofktrn
  • 56
  • 3
  • thanks a lot. So, how can i Know the TZ of a string? for example "2020-11-01T03:17:54Z, ¿where can I See is UTC? ¿Is it somewhere hidden in the text? – Gustavo Zárate Dec 02 '20 at 19:06
  • Actually the T separates the date from the time and the Z ("Zulu") in the end means UTC – sofktrn Dec 02 '20 at 19:52
  • You can also verify/see that tz is UTC by applying pd.to_datetime on the date, as shown in the answer:) – sofktrn Dec 02 '20 at 20:18