how to get time with am and pm in javascript code example

Example 1: js get time in am

var time = new Date();
console.log(
  time.toLocaleString('en-US', { hour: 'numeric', hour12: true })
);

Example 2: how to convert time to am pm in javascript

var suffix = hour >= 12 ? "PM":"AM";
var hours = ((hour + 11) % 12 + 1) + suffix

Example 3: date gethours am pm

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;
}

console.log(formatAMPM(new Date));