How to convert the current date to an era timestamp?

How to convert current date to timestamp?

Format current date:

29.08.2011 11:05:02 
+45
python
Aug 30 '11 at 9:29 a.m.
source share
6 answers

It 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 
+68
Aug 30 2018-11-11T00:
source share

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")); 
+10
Aug 30 2018-11-11T00:
source share
 Your code will behave strange if 'TZ' is not set properly, eg '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 
+9
Jan 6 '15 at 9:06
source share
 import time def expires(): '''return a UNIX style timestamp representing 5 minutes from now''' return int(time.time()+300) 
+4
Aug 30 2018-11-11T00:
source share

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

+2
Aug 30 2018-11-11T00:
source share

if you want UTC to 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 
+2
Feb 16 '17 at 20:23
source share



All Articles