0

I know there are a lot of answers to this question online, but none of them have worked for me. I am trying to convert a date string into a Datetime object, of the following format: yyyy-mm-dd

My date_string is '2017-02-02T00:00:00Z'

I am trying to convert it by doing date_value = datetime.datetime.strptime(date_string, '%Y%m%d') but I'm getting the following error:

ValueError: time data '"2017-02-02T00:00:00Z"' does not match format '%Y%m%d'

Also, should I be worried about the double quotes around my date_string string?

4
  • Does this answer your question? How do I parse an ISO 8601-formatted date? -> esp. stackoverflow.com/a/62769371/10197418 ;-) Commented Jun 2, 2021 at 12:43
  • Your date/time string pattern is incorrect. It doesn't account for the time component at all. Commented Jun 2, 2021 at 12:43
  • @MrFuppes Thank you for the link. I have tried the suggestion date = datetime.fromisoformat("2017-02-02T00:00:00.Z") but I get an error ValueError: Invalid isoformat string: '"2017-02-02T00:00:00.Z"' Commented Jun 2, 2021 at 13:13
  • Did you check out the second link in my comment? Commented Jun 2, 2021 at 14:21

2 Answers 2

1

The second argument in the method strptime is the pattern of your string.

Here is the full list of available code formats. https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes

All the remaining "non-informative" characters in your string can simply be put as-is in there correct places.

Thanks to @MrFuppes for this info: you should also parse the trailing "Z" as %z. This will signal python that it's a UTC datetime and not a local datetime.

Your code should be :

date_string = '2017-02-02T00:00:00Z'
date_value = datetime.datetime.strptime(date_string, '%Y-%m-%dT%H:%M:%S%z')

As for the extra quotes, that's not wanted. You should try this beforehand :

date_string = date_string.strip("'").strip('"')

If strip() didn't work, you can call eval instead (usually not recommended) :

date_string = eval(date_string)
Sign up to request clarification or add additional context in comments.

5 Comments

Z should not be parsed with a literal "Z" in the parsing directive. Why? This results in naive datetime, which Python treats as local time, while Z signals UTC. And secondly, using eval for something that can be done with a simple replace is inappropriate.
Thanks a lot for the informative Z. Although replace is less appropriate than strip IMO, I agree with you about eval.
Thank you for helping. I've tried the same sequence as you but I'm still getting a similar error (even after stripping the quotes): ValueError: time data '"2017-02-02T00:00:00Z"' does not match format '%Y-%m-%dT%H:%M:%S%z'
That's because the character (quote or doublequote) you're trying to remove should be the inner one shown in the Exception. So it would be .strip('"'). You can actually chain the strip method to handle both cases. I have updated my answer.
Thank you! There was another slight issue with regards to comparing "offset-naive" and "offset-aware" which threw an error, but otherwise this works! Thank you.
1

The solution is to parse your date_string first, and that should help. Using strptime() right away on an unparsed datetime string can sometimes cause problems. Also you shouldn't worry about your double quotes, it's fine.

First, install the python-dateutil library if you haven't already (pip install python-dateutil at the command line). Then test the solution with the following code.

import datetime
import dateutil.parser

date_string = '2017-02-02T00:00:00Z'

#we parse the string, it becomes a datetime object
parsed_date_string = dateutil.parser.parse(date_string)
print(parsed_date_string)

#output looks like this: 2017-02-02 00:00:00+00:00

#now your statement will work
date_value = datetime.datetime.strptime(str(parsed_date_string), '%Y-%m-%d %H:%M:%S%z')
print(date_value)

#output will also be: 2017-02-02 00:00:00+00:00

The strptime() statement worked this time because we parsed our date first with parse(). Note also that to use strptime() we need to cast our parsed_date_string back to a string because parse() converts our original string to an object of class datetime.datetime and strptime() is expecting a string.

Hopefully that helped.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.