2

I have stumbled upon a coding question where I have to convert a timezone-aware string like 2021-11-01T02:08:13.000Z to a Python datetime object.

I have seen many examples where timezone aware string is like in the format 2012-11-01T04:16:13-04:00, but for me as you can see the string is little different with the ".000Z" at the end.

I tried the below code:

from datetime import datetime

format = '%Y-%m-%dT%H:%M:%S%z'
time='2012-11-01T04:16:13.000Z'
date_time_obj = datetime.strptime(time, format)
print ("date",date_time_obj)

But I'm getting the error:

ValueError: time data '2012-11-01T04:16:13.000Z' does not match format '%Y-%m-%dT%H:%M:%S%z'

also tried doing like this:

from datetime import datetime
time='2012-11-01T04:16:13.000Z'
date_time_obj = datetime.fromisoformat(time)

But I'm getting this error:

Errpr: Invalid ISO format string:

I gone through many pages but was unable to find that particular format and how to convert it to a datetime aware string. Can anyone please help.

Andy A.
  • 1,392
  • 5
  • 15
  • 28
Thommac
  • 21
  • 2
  • The `.000` is fractions of a second; the `Z` is the timezone UTC – Jiří Baum Jul 12 '21 at 05:30
  • Python supports microseconds (`.000000`) but not directly milliseconds (`.000`); you'll have to either pull them out and add them separately, or insert another three zeros to turn them into microseconds, or if they're always zero you can match them as literal characters in the format – Jiří Baum Jul 12 '21 at 05:34
  • ok.. But how the formatting works. What is the correct way to convert this ? sorry I am in a dark here. – Thommac Jul 12 '21 at 05:34
  • Are the milliseconds always zero? – Jiří Baum Jul 12 '21 at 05:38
  • @sabik Python *does* support milliseconds (to be parsed by `%f`); the `Z` is the problem here. You can simply use `date_time_obj = datetime.fromisoformat(time.replace('Z', '+00:00'))` for example. Or parse with `%z`. – FObersteiner Jul 12 '21 at 20:58
  • @MrFuppes - The `%f` directive parses microseconds (six digits), not milliseconds (three digits) – Jiří Baum Jul 12 '21 at 23:18
  • @sabik please have a look at subsection 5 of the [technical detail](https://docs.python.org/3/library/datetime.html#technical-detail) paragraph. `%f` actually parses *fractional seconds* up to 6 decimal places (microseconds). So milliseconds (3 decimal places) also work very well; give it a try. – FObersteiner Jul 13 '21 at 07:24
  • Thanks, TIL! The `%f` directive can indeed do milliseconds – Jiří Baum Jul 13 '21 at 07:36

2 Answers2

2

You can do all of this using 'dateutil.parser.isoparse'. No need to strip out information (dropping the milliseconds), no need to fuss with the formatting string.

from dateutil import parser
time='2012-11-01T04:16:13.000Z'
date_time_obj = parser.isoparse(time)
print ("date",date_time_obj)

> date 2012-11-01 04:16:13+00:00
Josh
  • 136
  • 6
  • This works perfectly fine , but is there any way to solve this without using any predefined libraries? Just asking – Thommac Jul 12 '21 at 06:14
0

You may change your format like this:

format = '%Y-%m-%d %H:%M:%S'

And convert time like you format by doing this:

time='2012-11-01T04:16:13.000Z'
time=time.replace("T"," ").split(".")[0]

Or you may format it like

format = '%Y-%m-%dT%H:%M:%S.%f'

And you don't have to do anything for time now.

time='2012-11-01T04:16:13.000Z'
imxitiz
  • 3,920
  • 3
  • 9
  • 33