Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert 12-hour hh:mm AM/PM to 24-hour hh:mm

Is there any simple way to convert 12-hour hh:mm AM/PM to 24-hour hh:mm using jquery?

Note: not using any other libraries.

I have a var time = $("#starttime").val() that returns a hh:mm AM/PM.

like image 830
Rowie Po Avatar asked Feb 26 '13 07:02

Rowie Po


1 Answers

Try this

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); 
like image 106
devnull69 Avatar answered Oct 20 '22 08:10

devnull69