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

A

andydtaylor

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
 
M

MRAB

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)]
 
V

Vlastimil Brom

2013/3/2 said:
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

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
 
C

Cameron Simpson

| 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,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top