我的时间格式为“07:30 PM”。我需要使用javascript转换为24小时格式进行进一步计算。请协助我这样做。
答案 0 :(得分:0)
重复 this
简单的解决方案:
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
}