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

Discussion in 'Python' started by andydtaylor@gmail.com, Mar 2, 2013.

1. ### Guest

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'

Thanks,

Andy

, Mar 2, 2013

2. ### MRABGuest

On 2013-03-02 00:12, 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)]

MRAB, Mar 2, 2013

3. ### Vlastimil BromGuest

2013/3/2 <>:
> 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'
>
> Thanks,
>
>
> Andy
> --
> http://mail.python.org/mailman/listinfo/python-list

Hi,
you may use e.g. gmtime and only take the hours and minutes part into
account; the drawback is the range calculation in seconds:

>>> [time.strftime("%I:%M%p", time.gmtime(s)) for s in range(0, 86401, 1800)]

['12:00AM', '12:30AM', '01:00AM', '01:30AM', '02:00AM', '02:30AM',
'03:00AM', '03:30AM', '04:00AM', '04:30AM', '05:00AM', '05:30AM',
'06:00AM', '06:30AM', '07:00AM', '07:30AM', '08:00AM', '08:30AM',
'09:00AM', '09:30AM', '10:00AM', '10:30AM', '11:00AM', '11:30AM',
'12:00PM', '12:30PM', '01:00PM', '01:30PM', '02:00PM', '02:30PM',
'03:00PM', '03:30PM', '04:00PM', '04:30PM', '05:00PM', '05:30PM',
'06:00PM', '06:30PM', '07:00PM', '07:30PM', '08:00PM', '08:30PM',
'09:00PM', '09:30PM', '10:00PM', '10:30PM', '11:00PM', '11:30PM',
'12:00AM']
>>>

regards,
vbr

Vlastimil Brom, Mar 2, 2013
4. ### Cameron SimpsonGuest

On 01Mar2013 16:12, <> wrote:
| 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'

If they're just minutes to hours and minutes you don't need datetime;
it is most useful to handle the many complexities of calendars.

There are 60 minutes to an hour. So something like (untested):

for n in range(0,48): # counts 0..47 inclusive
minutes = 30 * (n + 1)
hours = minutes // 60
minutes = minutes % 60
if hours >= 12:
ampm = 'pm'
hours -= 12
else:
ampm = 'am'
if hours < 1:
hours += 12
print '%02d:%02d %s" % (hours, minutes, ampm)

Cheers,
--
Cameron Simpson <>

The Borg assimilated my race and all I got was this lousy tagline.
- Cath Lawrence <>

Cameron Simpson, Mar 2, 2013
5. ### Guest

Brilliant, thanks to all

, Mar 2, 2013
6. ### Guest

Brilliant, thanks to all

, Mar 2, 2013