datetime.time convert hours place to minutes place

Question:

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?

Asked By: X.T

||

Answers:

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?

Answered By: deceze
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.