Python provides different packages (datetime, time, calendar) as can be seen here in order to deal with time. I made a big mistake by using the following to get current GMT time time.mktime(datetime.datetime.utcnow().timetuple())

What is a simple way to get current GMT time in Unix timestamp?

3

10 Answers

I would use time.time() to get a timestamp in seconds since the epoch.

import time time.time() 

Output:

1369550494.884832 

For the standard CPython implementation on most platforms this will return a UTC value.

7
import time int(time.time()) 

Output:

1521462189 
0

Does this help?

from datetime import datetime import calendar d = datetime.utcnow() unixtime = calendar.timegm(d.utctimetuple()) print unixtime 

How to convert Python UTC datetime object to UNIX timestamp

2

python2 and python3

it is good to use time module

import time int(time.time()) 

1573708436

you can also use datetime module, but when you use strftime('%s'), but strftime convert time to your local time!

python2

from datetime import datetime datetime.utcnow().strftime('%s') 

python3

from datetime import datetime datetime.utcnow().timestamp() 
5

Python 3 seconds with microsecond decimal resolution:

from datetime import datetime print(datetime.now().timestamp()) 

Python 3 integer seconds:

print(int(datetime.now().timestamp())) 

WARNING on datetime.utcnow().timestamp()!

datetime.utcnow() is a non-timezone aware object. See reference:

For something like 1am UTC:

from datetime import timezone print(datetime(1970,1,1,1,0,tzinfo=timezone.utc).timestamp()) 

or

print(datetime.fromisoformat('1970-01-01T01:00:00+00:00').timestamp()) 

if you remove the tzinfo=timezone.utc or +00:00, you'll get results dependent on your current local time. Ex: 1am on Jan 1st 1970 in your current timezone - which could be legitimate - for example, if you want the timestamp of the instant when you were born, you should use the timezone you were born in. However, the timestamp from datetime.utcnow().timestamp() is neither the current instant in local time nor UTC. For example, I'm in GMT-7:00 right now, and datetime.utcnow().timestamp() gives a timestamp from 7 hours in the future!

Or just simply using the datetime standard module

In [2]: from datetime import timezone, datetime ...: int(datetime.now(tz=timezone.utc).timestamp() * 1000) ...: Out[2]: 1514901741720 

You can truncate or multiply depending on the resolution you want. This example is outputting millis.

If you want a proper Unix timestamp (in seconds) remove the * 1000

5

At least in python3, this works:

>>> datetime.strftime(datetime.utcnow(), "%s") '1587503279' 
1

I like this method:

import datetime, time dts = datetime.datetime.utcnow() epochtime = round(time.mktime(dts.timetuple()) + ) 

The other methods posted here are either not guaranteed to give you UTC on all platforms or only report whole seconds. If you want full resolution, this works, to the micro-second.

1
from datetime import datetime as dt dt.utcnow().strftime("%s") 

Output:

1544524990 
3
#First Example: from datetime import datetime, timezone timstamp1 =int(datetime.now(tz=timezone.utc).timestamp() * 1000) print(timstamp1) 

Output: 1572878043380

#second example: import time timstamp2 =int(time.time()) print(timstamp2) 

Output: 1572878043

  • Here, we can see the first example gives more accurate time than second one.
  • Here I am using the first one.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy