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"
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"
First you need to use the following module from the following library:
from datetime import datetime
Then just do it:
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 now.hour
and start it.
Would this be what you wanted?
Using the strftime()
method with the format %H
and returning string
:
from datetime import datetime
obj = datetime.now()
hora = datetime.strftime( obj, "%H")
print(hora)
Using the hour
attribute of the object datetime
and returning a int
:
from datetime import datetime
obj = datetime.now()
print(obj.hour)