Home > Back-end >  datetime.time convert hours place to minutes place
datetime.time convert hours place to minutes place

Time:12-25

I have a set of data such as:

  1. datetime.time(3,1)
  2. datetime.time(5,22) ...

Due to data entry error the datetime.time(3,1) is supposed to be datetime.time(0,3,1), and the datetime.time(5,22) is supposed to be datetime.time(0,5,22). Basically the current number at the hour attribute should be at the minutes attribute, and the number at the minute attribute should be at the second attribute.

Is there any way to make the conversion in Python?

CodePudding user response:

Fairly straight forward:

t = datetime.time(3,1)
t = datetime.time(0, t.hour, t.minute)

Note that hours can only go up to 24, so any higher minutes would… have led to an error already? Be discarded?

And are you sure you aren't looking for a timedelta instead?

  • Related