6

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 ?

1
  • I suspect you misread the suggestions. A date doesn't have a time. Commented Jun 26, 2016 at 8:50

4 Answers 4

9

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.

Sign up to request clarification or add additional context in comments.

Comments

1

datetime.date only takes care of the date, you need to use datetime.datetime.

Comments

1

You are using the datetime.date object which doesn't contain any time.

You need to use 'datetime.datetime.now()'

Comments

1

Try this: will return a String with Hour,Minutes and Seconds

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

Comments

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.