bbrame bbrame - 7 months ago 43
Javascript Question

How do you display javascript datetime in 12 hour AM/PM format?

How do you display a javascript datetime object in the 12 hour format (AM/PM)?

Answer
function formatAMPM(date) {
  var hours = date.getHours();
  var minutes = date.getMinutes();
  var ampm = hours >= 12 ? 'pm' : 'am';
  hours = hours % 12;
  hours = hours ? hours : 12; // the hour '0' should be '12'
  minutes = minutes < 10 ? '0'+minutes : minutes;
  var strTime = hours + ':' + minutes + ' ' + ampm;
  return strTime;
}
Comments