Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python date time get the current time but with seconds and hour and minute

I am able to get the current time as this:

import datetime
str(datetime.date.today().strftime("%Y-%m-%d"))

that prints

2016-06-26

but i need to add the minutes and seconds and hour

i search on internet and people are suggesting using this:

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

but actually the %H:%M returns 00:00 always

can you help ?

like image 489
Marco Dinatsoli Avatar asked Jun 26 '16 08:06

Marco Dinatsoli


1 Answers

Use

>>> datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
'2016-06-26 14:15:44'

You can even use .today() instead of .now().

datetime.date.today() is of type <type 'datetime.date'> which basically returns only the date. That is why your hours and minutes when printed returns 00:00.

Hence, if you use datetime.datetime.now() this would return type <type 'datetime.datetime'> which is a datetime object, you'll get the date and the time.

like image 197
JRodDynamite Avatar answered Sep 30 '22 09:09

JRodDynamite