0

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.

user13763
  • 83
  • 7
  • 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 Answers1

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
}
Community
  • 1
  • 1
deadulya
  • 686
  • 6
  • 15