admin管理员组

文章数量:1202341

The function returns the time in 24 hour format.

function fomartTimeShow(h) {
    return h < 10 ? "0" + h + ":00" : h + ":00";
}

returns the time in 24 hour format. I want the time to be converted in 12 hour format.
Anyhelp would be greatly appreciated.
Thanks.

The function returns the time in 24 hour format.

function fomartTimeShow(h) {
    return h < 10 ? "0" + h + ":00" : h + ":00";
}

returns the time in 24 hour format. I want the time to be converted in 12 hour format.
Anyhelp would be greatly appreciated.
Thanks.

Share Improve this question edited Feb 29, 2012 at 11:05 ternaryOperator 8334 silver badges10 bronze badges asked Feb 29, 2012 at 10:53 saransaran 5955 gold badges17 silver badges28 bronze badges 2
  • 1 So h is just an integer between 0 and 23? What have you tried? – Felix Kling Commented Feb 29, 2012 at 10:56
  • If you do a lot of date/time parsing, it might be interesting to check out Datejs. – flitig Commented Feb 29, 2012 at 11:00
Add a comment  | 

6 Answers 6

Reset to default 14

Just use modulus 12:

function formatTimeShow(h_24) {
    var h = h_24 % 12;
    return (h < 10 ? '0' : '') + h + ':00';
}

Modulus (%) means divide and take remainder. For example 17 / 12 = 1 with remainder 5. So the result of 17 % 12 is 5. And hour 17 is hour 5 in 12-hour time.

But note that this function is not complete since it doesn't work for hour 0 (or hour 12). To fix it you have to add in another check for that:

function formatTimeShow(h_24) {
    var h = h_24 % 12;
    if (h === 0) h = 12;
    return (h < 10 ? '0' : '') + h + ':00';
}

Also note that you can add a meridian easily, by seeing whether the hour is less than 12 (am) or equal to/greater (pm):

function formatTimeShow(h_24) {
    var h = h_24 % 12;
    if (h === 0) h = 12;
    return (h < 10 ? '0' : '') + h + ':00' + (h_24 < 12 ? 'am' : 'pm');
}

Note: All of the above is assuming the parameter to this function is an integer between 0 and 23.

You could try something like the following:

function fomartTimeShow(h) { 
    var ampm = "am"
    if (h >= 12)
        ampm = "pm"
    h = h % 12;
    if (h == 0)
     h = 12;
    return h ":00" + ampm; 
}
function fomartTimeShow(h) {
    var s = (h % 24 < 12) ? "am" : "pm", h = h % 12 || 12;
    return (h < 10 ? "0" + h : h)  + ":00" + " " + s;
}

In case if you are getting time like this "16:26", "12:50", "05:23", etc. Then, you can easily convert them into 12 hour format like "4:26 PM", "12:50 PM", "05:23 AM", etc. with AM/PM through this function.

function convertTo12Hour(oldFormatTime) {
    console.log("oldFormatTime: " + oldFormatTime);
    var oldFormatTimeArray = oldFormatTime.split(":");

    var HH = parseInt(oldFormatTimeArray[0]);
    var min = oldFormatTimeArray[1];

    var AMPM = HH >= 12 ? "PM" : "AM";
    var hours;
    if(HH == 0){
      hours = HH + 12;
    } else if (HH > 12) {
      hours = HH - 12;
    } else {
      hours = HH;
    }
    var newFormatTime = hours + ":" + min + " " + AMPM;
    console.log(newFormatTime);
}

I'm pretty sure this will work as an even more concise formulaic version of Ben Lee's answer, including for the h=0 and h=12 cases:

function fomartTimeShow(h_24) {
    var h = ((h_24 + 11) % 12)+1;
    return (h < 10 ? '0' : '') + h + ':00';
}

or including am/pm:

function fomartTimeShow(h_24) {
    var h = ((h_24 + 11) % 12)+1;
    return (h < 10 ? '0' : '') + h + ':00' + (h_24 < 12 ? 'am' : 'pm');
}

Yet another Javascript function to convert 24 hour to 12 hour but unlike all the other answers on this question, this one is easy to read.

function timeTo12(time){

  var parts = time.split(':');
  var hours = parseInt(parts[0]);
  var minutes = parts[1];

  if(hours >= 12){
    var newhours = parseInt(hours) - 12;
    var meridiem = "pm";
  }else{
    var newhours = parseInt(hours);
    var meridiem = "am";
  }

  if(newhours == 0) newhours = "12";
  var newtime = newhours + ":" + minutes + " " + meridiem;

  return newtime;
}

本文标签: How to convert time from 24 hour format to 12 hour format using javascriptStack Overflow