Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python strftime not working with hours minutes and seconds

I am reading the official documentations here

https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior

and it states that I can use

%H and %M and %S for hours, minutes and seconds

I do this:

datetime.date.today().strftime("%Y-%m-%d %H:%M:%S")

and I always get

'2016-07-18 00:00:00'

where are the values ?

like image 517
Marco Dinatsoli Avatar asked Jul 18 '16 22:07

Marco Dinatsoli


People also ask

What is %f in timestamp?

This is mostly because for such objects, %f means "fiscal year".

How does Strftime work in Python?

The strftime() function is used to convert date and time objects to their string representation. It takes one or more input of formatted code and returns the string representation. Returns : It returns the string representation of the date or time object. List of format codes : Reference table for the format codes.

What is the difference between Strptime and Strftime?

strptime is short for "parse time" where strftime is for "formatting time". That is, strptime is the opposite of strftime though they use, conveniently, the same formatting specification.


1 Answers

You are asking for a date, which doesn't include a time. You want a datetime:

datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")

Example:

In [3]: datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
Out[3]: '2016-07-18 18:26:18'
like image 50
juanpa.arrivillaga Avatar answered Oct 05 '22 13:10

juanpa.arrivillaga