1

In python im trying to generate a timestamp based on a string input: The data

19/21/2016  12:29:07

First I tried:

import time
import datetime
s = "19/04/2016"
seconds = time.mktime(datetime.datetime.strptime(s, "%d/%m/%Y").timetuple())
print seconds

Which worked, than I tried (with same imports):

s = "19/04/2016 12:29:07"
seconds = time.mktime(datetime.datetime.strptime(s, "%d/%m/%Y %H:M:S").timetuple())
print seconds

But I get the following error:

ValueError: time data '19/04/2016 12:29:07' does not match format '%d/%m/%Y %H:M:S'

%H Hour (24-hour clock) as a zero-padded decimal number. %M Minute as a zero-padded decimal number. %S Second as a zero-padded decimal number.

Why is the input not valid when adding the %H:%M:%S ?

1 Answer 1

2

The problem is not the 24 hour clock. The format of the first string is different from the second in that the second has an impossible value for month - 21.

Update:

Each placeholder should have a preceeding %:

s = "19/04/2016 12:29:07"
format = "%d/%m/%Y  %H:%M:%S"
Sign up to request clarification or add additional context in comments.

1 Comment

Updated the question you were right that was a typo though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.