Python object datetime value extraction.

Asked

Viewed 1,145 times

2

I need to extract only the time of a datetime object. How to proceed? The object returns the following output: "3 days, 22:01:00"

2 answers

3

First you need to use the following module from the following library:

from datetime import datetime

then just do:

 now = datetime.now()

 print("%s:%s:%s" %(now.hour,now.minute,now.second))

output:

23:4:58

If you only want the time, just use the now.hour and imprint him.

this is what you wanted?

2

Using the method strftime() with formatting %H and returning string:

from datetime import datetime
obj = datetime.now()
hora = datetime.strftime( obj, "%H")
print(hora)

Using the attribute hour of the object datetime and returning a int:

from datetime import datetime
obj = datetime.now()
print(obj.hour)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.