Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Invalid format string [duplicate]

I am trying to print the date in the following format using strftime: 06-03-2007 05:40PM

I have written the following code:

import time
print time.strftime("%m-%d-%Y %T:%M%p")

But it gives an error "Invalid format string". Why is that?

like image 331
Shagufta Oliveyu Methwani Avatar asked Oct 01 '14 11:10

Shagufta Oliveyu Methwani


2 Answers

Maybe you are looking for hour, H instead of T. check strftime possible options

print time.strftime("%m-%d-%Y %H:%M%p")

Note: The behavior is different on Windows and Linux machine. If you see what all are supported by Windows platform, check this http://msdn.microsoft.com/en-us/library/fe06s4ak.aspx

So it is better to use %H:%M:%S than %T on Windows.

like image 79
sk11 Avatar answered Oct 10 '22 10:10

sk11


The %T in:

print time.strftime("%m-%d-%Y %T:%M%p")

Does not exsist. You should use %H (0-23) or %I (0-11)

like image 25
Vincent Beltman Avatar answered Oct 10 '22 09:10

Vincent Beltman