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
Post a Comment