datetime - How to get an UTC date string in Python? -


i trying utc date string "yyyymmdd"

for following,

nowtime =  time.gmtime(); nowdate = date(nowtime.tm_year, nowtime.tm_mon, nowtime.tm_mday) print nowdate.strftime('%y%m%d') 

i used do:

datetime.date.today().strftime() 

but gives me date string in local tz

how can utc date string?

from datetime import datetime, timezone datetime.now(timezone.utc).strftime("%y%m%d") 

or davidism pointed out, work:

from datetime import datetime datetime.utcnow().strftime("%y%m%d") 

i prefer first approach, gets in habit of using timezone aware datetimes - j.f. sebastian pointed out - requires python 3.2+. second approach work in both 2.7 , 3.2 branches.


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -