Aditya Jha
Aditya Jha

Reputation: 69

Error in converting datetime from string to datetime object in python

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?

Upvotes: 0

Views: 61

Answers (1)

DeepSpace
DeepSpace

Reputation: 81594

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..)

Upvotes: 1

Related Questions