How to convert current date to epoch timestamp?

Python

Python Problem Overview


How to convert current date to epoch timestamp ?

Format current date:

29.08.2011 11:05:02

Python Solutions


Solution 1 - Python

That should do it

import time

date_time = '29.08.2011 11:05:02'
pattern = '%d.%m.%Y %H:%M:%S'
epoch = int(time.mktime(time.strptime(date_time, pattern)))
print epoch

Solution 2 - Python

Your code will behave strange if 'TZ' is not set properly, e.g. 'UTC' or 'Asia/Kolkata'

So, you need to do below

>>> import time, os
>>> d='2014-12-11 00:00:00'
>>> p='%Y-%m-%d %H:%M:%S'
>>> epoch = int(time.mktime(time.strptime(d,p)))
>>> epoch
1418236200
>>> os.environ['TZ']='UTC'
>>> epoch = int(time.mktime(time.strptime(d,p)))
>>> epoch
1418256000

Solution 3 - Python

Assuming you are using a 24 hour time format:

import time;
t = time.mktime(time.strptime("29.08.2011 11:05:02", "%d.%m.%Y %H:%M:%S"));

Solution 4 - Python

import time
def expires():
    '''return a UNIX style timestamp representing 5 minutes from now'''
    return int(time.time()+300)

Solution 5 - Python

if you want UTC try some of the gm functions:

import time
import calendar

date_time = '29.08.2011 11:05:02'
pattern = '%d.%m.%Y %H:%M:%S'
utc_epoch = calendar.timegm(time.strptime(date_time, pattern))
print utc_epoch

Solution 6 - Python

from time import time
>>> int(time())
1542449530


>>> time()
1542449527.6991141
>>> int(time())
1542449530
>>> str(time()).replace(".","")
'154244967282'

But Should it not return ?

'15424495276991141'

Solution 7 - Python

Use strptime to parse the time, and call time() on it to get the Unix timestamp.

Solution 8 - Python

Short-hand to convert python date/datetime to Epoch (without microseconds)

int(current_date.strftime("%s")) # 2020-01-14  ---> 1578956400
int(current_datetime.strftime("%s")) # 2020-01-14 16:59:30.251176 -----> 1579017570

Solution 9 - Python

enter image description hereI think this answer needs an update and the solution would go better this way.

from datetime import datetime

datetime.strptime("29.08.2011 11:05:02", "%d.%m.%Y %H:%M:%S").strftime("%s")

or you may use datetime object and format the time using %s to convert it into epoch time.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBdfyView Question on Stackoverflow
Solution 1 - PythonPierre LacaveView Answer on Stackoverflow
Solution 2 - PythonMohammad Shahid SiddiquiView Answer on Stackoverflow
Solution 3 - PythonPaulView Answer on Stackoverflow
Solution 4 - PythonMalView Answer on Stackoverflow
Solution 5 - PythonKevin KreiserView Answer on Stackoverflow
Solution 6 - PythonaksView Answer on Stackoverflow
Solution 7 - PythonSjoerdView Answer on Stackoverflow
Solution 8 - PythonpymenView Answer on Stackoverflow
Solution 9 - PythonAkarsh JainView Answer on Stackoverflow