Extraction of datetime object value in Python.

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"

    
asked by anonymous 19.10.2017 / 01:49

2 answers

3

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?

    
19.10.2017 / 03:07
2

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)
    
19.10.2017 / 04:53