0

I am converting a date in the string to datetime object like this

datetime_object = datetime.strptime('2019-07-01T07:52:48.337-0700', '%Y-%m-%dT%H:%M:%S.%f-%Z')

Here I'm not understanding what the 0700 is? Is it timezone? Isn't %Z or %z the right directive for that?

1 Answer 1

1

The - is part of the timezone offset -0700 (the %z directive, lowercase z), you should not have it in the format string:

datetime_object = datetime.strptime('2019-07-01T07:52:48.337-0700',
                                    '%Y-%m-%dT%H:%M:%S.%f%z') # no - between %f and %z

%Z (uppercase Z) is for timezone name (UTC, EST, etc..)

Sign up to request clarification or add additional context in comments.

3 Comments

It says ValueError: 'z' is a bad directive in format '%Y-%m-%dT%H:%M:%S.%f%z'
Oh just got to know that Python 2 does't support offsets. Thanks.
@AdityaJha Then consider upgrading to Python 3

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.