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
    #1
    1. Advertising

  2. MRAB Guest

    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
    #2
    1. Advertising

  3. 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
    #3
  4. 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
    #4
  5. Guest

    Brilliant, thanks to all
     
    , Mar 2, 2013
    #5
  6. Guest

    Brilliant, thanks to all
     
    , Mar 2, 2013
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tshad
    Replies:
    7
    Views:
    17,264
    tshad
    Mar 11, 2005
  2. 00_CP_D12
    Replies:
    3
    Views:
    9,059
    dilipv
    Feb 22, 2008
  3. `p
    Replies:
    7
    Views:
    409
    Steve Litt
    Dec 14, 2005
  4. Replies:
    1
    Views:
    578
    Roedy Green
    Aug 13, 2012
  5. lemonhead71
    Replies:
    0
    Views:
    1,726
    lemonhead71
    Oct 3, 2012
Loading...

Share This Page