import time from datetime import datetime d = datetime.strptime("25.05.2017 15:56:53", "%d.%m.%Y %H:%M:%S").strftime("%s") print d 

This code gives an error

 ValueError: Invalid format string 

How to make everything work?

    1 answer 1

    1. The datetime.strptime("25.05.2017 15:56:53", "%d.%m.%Y %H:%M:%S") works well, and converts the text representation to a datetime object of the form datetime.datetime(2017, 5, 25, 15, 56, 53)
    2. About the error. Here is the description of the formatting "tags" for the strftime() and strptime() methods: strftime () and strptime () Behavior . There is no %s in the table there that you are trying to use for formatting. Hence the error.
    3. To convert a datetime object to a timestamp , you can use the following code:

       import time from datetime import datetime d = datetime.strptime("25.05.2017 15:56:53", "%d.%m.%Y %H:%M:%S") ts = time.mktime(d.timetuple()) print ts # 1495717013.0 
    • EMNIP, mktime relies on a system time zone? - andreymal
    • @andreymal, the docks say that mktime() inverse localtime() . time.localtime() returns the time according to the system time zone, as opposed to time.gmtime() (which returns the time in UTC). Well, in theory, mktime() will return the number of seconds that have elapsed since 01.01.1970 00:00:00 UTC, taking into account the system time zone. - insolor