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?
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?
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)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.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 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. - insolorSource: https://ru.stackoverflow.com/questions/670836/
All Articles