jastify
jastify

Reputation: 31

datetime.strptime() not returning correct timezone info, only recognizes UTC as a proper time zone name

When I run:

datetime.strptime('UTC', '%Z')

It compiles just fine, because as specified here the representation for time zone abbreviations is '%Z'

But if I run...

datetime.strptime('EST', '%Z')

I get a ValueError, 'EST' does not match the format '%Z', even though like before 'EST' is even provided as an example for %Z at the same link I provided above

What am I doing wrong?

Upvotes: 2

Views: 484

Answers (2)

Zaman Afzal
Zaman Afzal

Reputation: 2109

from datetime import datetime, timedelta
from pytz import timezone
import pytz
eastern = timezone('US/Eastern')
loc_dt = eastern.localize(datetime(2002, 10, 27, 6, 0, 0))
print(loc_dt.strftime('%Z'))

For refrence Please have a look at this link (http://pytz.sourceforge.net/)!

Upvotes: 1

neverwalkaloner
neverwalkaloner

Reputation: 47374

If you need to work with timezones use pytz

from pytz import timezone
str(datetime.datetime.now(timezone('EST')))

Upvotes: 0

Related Questions