Home > Back-end >  How split the time in python
How split the time in python

Time:02-28

I want to split the time for having the hours, the minutes, and the second on a list. But with the datetime module I can't. This is my code :

heure = datetime.now().time()
heure = heure.split(":")
print(heure)

Thank you in advance.

CodePudding user response:

heure is a datetime.time object, not a string. It already splits out the various components of the time as properties. Do:

>>> hms = [heure.hour, heure.minute, heure.second]
>>> hms
[8, 9, 33]

CodePudding user response:

You need to convert the heure to string with the str() and then split it.

heure = datetime.now().time()
heure = str(heure).split(":")
print(heure)
  • Related