Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

convert 24 hours to 12 hours javascript

function tConvert (time) {
  // Check correct time format and split into components
  time = time.toString ().match (/^([01]d|2[0-3])(:)([0-5]d)(:[0-5]d)?$/) || [time];

  if (time.length > 1) { // If time format correct
    time = time.slice (1);  // Remove full string match value
    time[5] = +time[0] < 12 ? 'AM' : 'PM'; // Set AM/PM
    time[0] = +time[0] % 12 || 12; // Adjust hours
  }
  return time.join (''); // return adjusted time or original string
}

tConvert ('18:00:00');
Comment

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)}`);
Comment

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" && hours<12) hours = hours+12;
if(AMPM == "AM" && hours==12) hours = hours-12;
var sHours = hours.toString();
var sMinutes = minutes.toString();
if(hours<10) sHours = "0" + sHours;
if(minutes<10) sMinutes = "0" + sMinutes;
alert(sHours + ":" + sMinutes);

Comment

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


Comment

PREVIOUS NEXT
Code Example
Javascript :: convert jquery to javascript converter online tool 
Javascript :: ampscript remove special character 
Javascript :: newtonsoft json parse string 
Javascript :: var vs let javascript 
Javascript :: router.push 
Javascript :: window location href 
Javascript :: js DFS 
Javascript :: how to take input from user in javascript console 
Javascript :: how to upload file in node js 
Javascript :: how to remove duplicates in js 
Javascript :: nextjs apollo client 
Javascript :: javascript return multiple values 
Javascript :: how to focus out of an input in testing library 
Javascript :: how to reverse sort lines in javascript 
Javascript :: rem api rest 
Javascript :: javascript handle updation of copy object 
Javascript :: array of numbers to array of objects 
Javascript :: javascript Symbols are not included in for...in Loop 
Javascript :: javascript typeof operator returns function 
Javascript :: how to locate an object with a spcific key in js array 
Javascript :: alphanumeric without space regex 
Javascript :: how to check if a number is divisible by 3 and 5 in javascript 
Javascript :: Javascript: take every nth Element of Array 
Javascript :: Cntrlsss:$.Control-Ai 
Javascript :: phaser mixed animation 
Javascript :: Who likes it 
Javascript :: hook use effect with class 
Javascript :: nestjs TS2339 
Javascript :: event.target javascript 
Javascript :: add line break in innerhtml 
ADD CONTENT
Topic
Content
Source link
Name
5+1 =