How to get an UTC date string in Python? [duplicate]

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

Or as Davidism pointed out, this would also work:

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

I prefer the first approach, as it gets you in the habit of using timezone aware datetimes – but as J.F. Sebastian pointed out – it requires Python 3.2+. The second approach will work in both 2.7 and 3.2 branches.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)