2

I am getting the error above when I do:

datetime.strptime(item['dt'], "%Y-%m-%dT%H:%M:%S.%fZ")

Even if I try:

datetime.strptime(item['dt'], "%Y-%m-%dT%H:%M:%S.%f")

I am getting unconverted data remains: -04:00 as my error.

What am I doing wrong?

nb_nb_nb
  • 1,243
  • 11
  • 36

2 Answers2

2

it's because you have 04:00 instead of 0400 for the utc offset...try this:

datetime.datetime.strptime('2019-06-02T16:19:27.000-0400', "%Y-%m-%dT%H:%M:%S.%f%z")

output:

datetime.datetime(2019, 6, 2, 16, 19, 27, tzinfo=datetime.timezone(datetime.timedelta(-1, 72000)))

%z is expecting something like hhmm

https://docs.python.org/3/library/datetime.html - see format codes

Derek Eden
  • 4,403
  • 3
  • 18
  • 31
  • `dt.datetime.strptime('2019-06-02T16:19:27.000-04:00', "%Y-%m-%dT%H:%M:%S.%f%z")` fails. You're suggesting instead that they have to edit out the final colon for every datetime? – roganjosh Dec 10 '19 at 00:14
  • I'm pointing out why it's failing and how out it's supposed to be. If they want to use this method then the inputs have to be formatted properly. Also, editing the strings wouldn't be overly difficult anyways. – Derek Eden Dec 10 '19 at 00:17
  • Sure, but with a million strings, it could be quite costly :) I'm not sure `dateutil` is gonna handle this though, or whether you can tag a `pytz` component on the end, so it may well be the most efficient approach – roganjosh Dec 10 '19 at 00:20
-1

According to the format codes for datetime.strptime, you should use %z to denote an UTC offset instead:

datetime.strptime('2019-06-02T16:19:27.000-04:00', "%Y-%m-%dT%H:%M:%S.%f%z")
blhsing
  • 91,368
  • 6
  • 71
  • 106
  • I am still getting this error: `time data '2019-06-02T16:19:27.000-04:00' does not match format '%Y-%m-%dT%H:%M:%S.%f%z'` – nb_nb_nb Dec 09 '19 at 23:59
  • Perhaps you have a typo somewhere. Please see demo: https://repl.it/@blhsing/SupportiveHarmoniousRuby – blhsing Dec 10 '19 at 00:00
  • This is what I have: `for item in db_mongo:` `created_dt = datetime.strptime(item['created_dt'], "%Y-%m-%dT%H:%M:%S.%f%z")` – nb_nb_nb Dec 10 '19 at 00:03
  • Which Python version are you using? – blhsing Dec 10 '19 at 00:04
  • I am using Python 3.6.8 – nb_nb_nb Dec 10 '19 at 00:06
  • I see. You have a colon in the UTC offset in your input so it has to be parsed by Python 3.7+. According to the documentation, "Changed in version 3.7: When the `%z` directive is provided to the `strptime()` method, the UTC offsets can have a colon as a separator between hours, minutes and seconds. For example, `'+01:00:00'` will be parsed as an offset of one hour. In addition, providing `'Z'` is identical to `'+00:00'`". – blhsing Dec 10 '19 at 00:11
  • although there was some explanation in the comments.. I feel the answer should be edited/unaccepted given that it doesn't actually work for the OP python version..maybe just add a second answer showing the solution for python 3.6 and 3.7 for those who come across this page looking for a solution to a s imilar problem – Derek Eden Dec 10 '19 at 13:59