Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert current date to epoch timestamp?

Tags:

python

How to convert current date to epoch timestamp ?

Format current date:

29.08.2011 11:05:02 
like image 768
Bdfy Avatar asked Aug 30 '11 09:08

Bdfy


People also ask

How do you convert date to epoch time?

Convert from human-readable date to epochlong epoch = new java.text.SimpleDateFormat("MM/dd/yyyy HH:mm:ss").parse("01/01/1970 01:00:00").getTime() / 1000; Timestamp in seconds, remove '/1000' for milliseconds. date +%s -d"Jan 1, 1980 00:00:01" Replace '-d' with '-ud' to input in GMT/UTC time.

What is epoch date format?

The Unix epoch (or Unix time or POSIX time or Unix timestamp) is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT), not counting leap seconds (in ISO 8601: 1970-01-01T00:00:00Z).

How do you convert datetime to seconds since epoch?

To convert a datetime to seconds, subtracts the input datetime from the epoch time. For Python, the epoch time starts at 00:00:00 UTC on 1 January 1970. Subtraction gives you the timedelta object. Use the total_seconds() method of a timedelta object to get the number of seconds since the epoch.


1 Answers

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 
like image 106
Pierre Lacave Avatar answered Oct 20 '22 23:10

Pierre Lacave