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

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


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

Or as Davidism pointed out, this would also work:

from datetime import datetimedatetime.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.