Answers for "js 24 hour to 12 hour with am pm"

36

12 hours to 24 hours javascript

const time = '5:00AM';

function convertTo24HrsFormat(time) {
   const slicedTime = time.split(/(PM|AM)/gm)[0];

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

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

   let updateHourAndMin;

   function addition(hoursOrMin) {
      updateHourAndMin =
         hoursOrMin.length < 2
            ? (hoursOrMin = `${0}${hoursOrMin}`)
            : hoursOrMin;

      return updateHourAndMin;
   }

   if (time.endsWith('PM')) {
      hours = parseInt(hours, 10) + 12;
   }

   return `${addition(hours)}:${addition(minutes)}`;
}

console.log(`Converted time: ${convertTo24HrsFormat(time)}`);
Posted by: Guest on January-12-2022
0

12 hours to 24 hours javascript

var time = $("#starttime").val();
var hours = Number(time.match(/^(\d+)/)[1]);
var minutes = Number(time.match(/:(\d+)/)[1]);
var AMPM = time.match(/\s(.*)$/)[1];
if(AMPM == "PM" &amp;&amp; hours&lt;12) hours = hours+12;
if(AMPM == "AM" &amp;&amp; hours==12) hours = hours-12;
var sHours = hours.toString();
var sMinutes = minutes.toString();
if(hours&lt;10) sHours = "0" + sHours;
if(minutes&lt;10) sMinutes = "0" + sMinutes;
alert(sHours + ":" + sMinutes);
Posted by: Guest on February-14-2022

Code answers related to "js 24 hour to 12 hour with am pm"

Code answers related to "Javascript"

Browse Popular Code Answers by Language