Warm tip: This article is reproduced from serverfault.com, please click

How to get the time and convert it to UTC in Python

发布于 2020-11-30 22:18:15

I'm trying to build an app where I can an API request, but I want to send a time query where the time is the time 10 minutes ago. How should I go about doing that in Python?

Questioner
Evan Krause
Viewed
0
Achintya Kumar 2020-12-05 02:37:15

Something like this should do the trick.

datetime.now(timezone.utc)

Alternatively, you could try this as well. For me, I had to add 5 hours because I'm in EST, so do the respective conversion.

curTime = (datetime.datetime.now() + datetime.timedelta(hours=5)).strftime('%Y-%m-%d %H:%M:%S')