Best way to convert number of minutes to hh:mm AM/PM?

MRAB python at mrabarnett.plus.com
Fri Mar 1 19:33:29 EST 2013


On 2013-03-02 00:12, andydtaylor at gmail.com wrote:
> Hi,
>
> I need to create a list of equally spaced times (as in hh:mm AM/PM) within a day to loop through. Having selected 30 minute intervals I figured I could:
>
> * Create a list from 1 to 48
> * Multiply each value by 30
> * Convert minutes to a time. datetime.timedelta seems to do this, but it's not a full timestamp which means strftime can't format me a time with am/pm.
>
> can anyone suggest a good approach to use? Ultimately I'd like to generate an equivalent to this text/format:'2:30 pm'
>
You can still use strftime, just pick a day but don't include it in the 
format.

You can make the list like this:

[(datetime.datetime(2000, 1, 1) + datetime.timedelta(minutes=30) * 
i).strftime("%H:%M%p") for i in range(48)]




More information about the Python-list mailing list