0

Possible Duplicate:
JavaScript seconds to time with format hh:mm:ss

Seems to me that this is a simple task, but I have not yet managed to make this work.

Essentially, I am starting with a string that contains an ISO-formatted time (HH:MM:SS) and I am wondering how to convert it to H:MM am|pm using JavaScript.

Any help is much appreciated.

Community
  • 1
  • 1
steacha
  • 171
  • 5
  • 13

4 Answers4

3

hello this may help you... its a jquery plugin called moments...

A lightweight (4.8k) javascript date library for parsing, manipulating, and formatting dates.

Moment.js

pl4g4
  • 101
  • 4
3

This simple function should do the trick:

function convertTime (isoTime) {
  var hours   = parseInt(isoTime.substring(0, 2), 10),
      minutes = isoTime.substring(3, 5),
      ampm    = 'am';

  if (hours == 12) {
    ampm = 'pm';
  } else if (hours == 0) {
    hours = 12;
  } else if (hours > 12) {
    hours -= 12;
    ampm = 'pm';
  }

  return hours + ':' + minutes + ' ' + ampm;
}

Should be self-explanatory. See this jsfiddle for example: http://jsfiddle.net/nscXP/3/

Travis
  • 12,001
  • 8
  • 39
  • 52
  • 1
    This solution will mark `"12:34:45"` as being in the "am". As I noted above, the noon hour qualifies as a special case that needs to be dealt with individually. – Pete Sep 06 '12 at 19:48
  • You are right @Pete. I fixed my code to deal with the noon hour individually. – Travis Sep 06 '12 at 19:53
  • You're still having issues with midnight there. Try "00:12:34" and see what happens. :-) *(almost there!)* – Pete Sep 06 '12 at 19:58
  • Fixed (again). Should I change my wording from this "simple" function haha. – Travis Sep 06 '12 at 20:01
  • 1
    one more bug, See what happens when you put in `"01:23:45"` -- it will give you `"01:23 am"`, while `"13:23:45"` will return `"1:23 pm"`. You should force `hours` to be a number to remove the leading `0`. (Pretty soon you'll just have my solution there, hehe). – Pete Sep 06 '12 at 20:06
  • Yep, that's why I upvoted yours :-) – Travis Sep 06 '12 at 20:10
  • Awe, how nice of you! :-) Put in that last little fix and I'll be happy to upvote you for effort! – Pete Sep 06 '12 at 20:11
  • Thanks guys. I removed the 'parseInt' from 'minutes' so that I could actually end up with the leading zeros. Works like a charm! :) – steacha Sep 07 '12 at 11:03
2

Just because there are solutions coming in that will fail to handle the noon hour or midnight hour properly, I'm going to toss my hat in the ring, even though it's not that different from what's been offered (aside than the fact it works properly):

var isoToCommonTime = function(iso) {
    var groups = iso.match(/^(\d\d):(\d\d)/),
        hour = +groups[1],
        ampm = (hour >= 12) ? "pm" : "am";

    if(hour > 12) {
        hour -= 12;
    } else if(hour === 0) {
        hour = 12;
    }

    return hour + ":" + groups[2] + " " + ampm;
};
Pete
  • 2,538
  • 13
  • 15
1

Something like this would work...

input = "12:34:45";
groups = input.match(/(.+):(.+):(.+)/);

if (groups[1] > 12) {
    hour = groups[1] - 12;
    unit = "pm";
} else {
    hour = groups[1];
    unit = "am";
}

minutes = groups[2];

Edit: There's some great points in the comments about issues with this at noon and midnight. I'll leave fixing these problems as an exercise to the reader. (I feel that the actual interesting part here is doing the string parsing)

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
  • Careful, I think your comparison operator should be `>` instead of `>=`. – Travis Sep 06 '12 at 19:42
  • 2
    @Travis is half right. `>= 12` is bad because it will turn `"12:34:45"` to `"00:34:45pm"`. There needs to be a second check to account for the special case of the noon hour (where it's "pm", but doesn't get the subtraction correction) – Pete Sep 06 '12 at 19:47