I have the time which is in the format "07:30PM" .I need to convert into 24 hr format using javascript to do further calculations.Please assist me in doing this.
Asked
Active
Viewed 2,297 times
0
-
SO is not a site where you can just ask for functionality. Please show us what you tried, and we'll be more than willing to put some effort into helping you figure out a solution. – Cerbrus Apr 30 '14 at 07:41
-
this should help you.. http://stackoverflow.com/questions/15083548/convert-12-hour-hhmm-am-pm-to-24-hour-hhmm – RicoWijaya Apr 30 '14 at 07:58
1 Answers
0
Duplicate of this
Simple solution:
function ampmTo24(time)
{
var hours = Number(time.match(/^(\d+)/)[1]);
var minutes = Number(time.match(/:(\d+)/)[1]);
var AP = time.match(/\s(.*)$/);
if (!AP) AP = time.slice(-2);
else AP=AP[1];
if(AP == "PM" && hours<12) hours = hours+12;
if(AP == "AM" && hours==12) hours = hours-12;
var Hours24 = hours.toString();
var Minutes24 = minutes.toString();
if(hours<10) Hours24 = "0" + Hours24;
if(minutes<10) Minutes24 = "0" + Minutes24;
return Hours24 + ":" + Minutes24
}
-
But time.match for AP var does not works as there is no space between (07:30AM) time and am/pm – user13763 Apr 30 '14 at 09:05
-
-
can u pls explain cant understand what is happening in the code is there any way without using match – user13763 Apr 30 '14 at 10:41