How to convert a date to timestamp in python?

Asked

Viewed 1,395 times

1

I have a certain date in a variable in that format d/m/Y. Behold:

strDate = "29/03/2017"

How to convert that date to timestamp in Python?

  • The timestamp has several formats: ISO-8601, rfc-2822, rfc-3339, North American, European, Unix epoch, POSIX time, big-endian and Custom formats: http://brito.blog.lumeinco.com.br/2017/08/timestamp.html - The most convenient is to convert the date string, in date objects. Then format the presentation as you wish. - There is an example in en.stackoverflow.com with three ways to solve this problem https://answall.com/a/224598/62736 at execution here. The solutions feature the use of datetime, time, and Arrow packages.

1 answer

3


>>> import time
>>> import datetime
>>> s = "01/12/2011"
>>> time.mktime(datetime.datetime.strptime(s, "%d/%m/%Y").timetuple())
1322697600.0

Would convert this data "01/12/2011" -> 1322697600

You can try this "simplified" way too:

>>> int(datetime.datetime.strptime('01/12/2011', '%d/%m/%Y').strftime("%s"))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.