12 hour format to 24 hour format javascript code example

Example 1: javascript conver time into 24 hour format

var dt = moment("12:15 AM", ["h:mm A"]).format("HH:mm");

Example 2: convert 12 hour format to 24 hour format in javascript

const convertTime12to24 = (time12h) => {
  const [time, modifier] = time12h.split(' ');

  let [hours, minutes] = time.split(':');

  if (hours === '12') {
    hours = '00';
  }

  if (modifier === 'PM') {
    hours = parseInt(hours, 10) + 12;
  }

  return `${hours}:${minutes}`;
}