AOL: Buy, Sell or Hold Micron at $445 or SanDisk at $885? Learn how to convert UTC date time to local date time with examples and solutions provided by the Stack Overflow community. datetime.now(timezone.utc) datetime.now(timezone.utc).timestamp() * 1000 # POSIX timestamp in milliseconds For your purposes when you need to calculate an amount of time spent between two dates all that you need is to subtract end and start dates.

Understanding the Context

The results of such subtraction is a timedelta object. From the python docs: datetime - How to get UTC time in Python? - Stack Overflow UTC, which stands for Coordinated Universal Time in English, is defined by atomic clocks, but is otherwise the same. In UTC a second always has the same length.

Key Insights

Leap seconds are inserted in UTC to keep UTC and GMT from drifting apart. By contrast, in GMT the seconds are stretched as necessary, so in principle they don’t always have the same ... Does time.time() in the Python time module return the system's time or the time in UTC? How do I convert a datetime string in local time to a string in UTC time? I'm sure I've done this before, but can't find it and SO will hopefully help me (and others) do that in future.

Final Thoughts

Clarifica... 37 Your goal shouldn't be to add a Z character, it should be to generate a UTC "aware" datetime string in ISO 8601 format. The solution is to pass a UTC timezone object to datetime.now() instead of using datetime.utcnow(): Python UTC datetime object's ISO format doesn't include Z (Zulu or Zero ... I feel neccessary to add comment about "why" exactly this behaviour was implemented the way it was - it's because Postgresql doesn't actually store timezone even in "timestamp with timezone" type - it just assumes that input time is in UTC. So it's really easy to "shoot off your leg" in this one, especially when you use something like DateTime.Now More on that here - stackoverflow.com ...