How to convert a date to timestamp in python?

1

I have a certain date on a variable in this d/m/Y format. See:

strDate = "29/03/2017"

How to convert this date to timestamp in Python?

    
asked by anonymous 29.03.2017 / 17:37

2 answers

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" form too:

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

The timestamp has several formats: ISO-8601, rfc-2822, rfc-3339, North American, European, Unix epoch, POSIX time, big-endian and even custom formats. [ reference1 ]

The most convenient is to convert the date string to date objects. Then format the presentation as you want.

There is a example in the .stackoverflow.com with three ways to solve this problem at execution here . The solutions feature the use of datetime, time, and arrow packages.

    
01.08.2017 / 13:54