Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javascript将12小时时间格式转换为24小时格式_Javascript - Fatal编程技术网

使用javascript将12小时时间格式转换为24小时格式

使用javascript将12小时时间格式转换为24小时格式,javascript,Javascript,我的时间格式为晚上7:30。我需要使用javascript将其转换为24小时格式以进行进一步计算。请协助我完成此操作。副本 简单解决方案: 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.sl

我的时间格式为晚上7:30。我需要使用javascript将其转换为24小时格式以进行进一步计算。请协助我完成此操作。

副本

简单解决方案:

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
}

所以,这不是一个你可以只要求功能的网站。请向我们展示您所做的尝试,我们将非常愿意付出努力帮助您找到解决方案。这将对您有所帮助。。但是AP var的time.match不起作用,因为在上午7:30和上午/下午7:30之间没有空间编辑时间(有或没有空间)。你能解释一下吗?我不能理解代码中发生了什么,有没有不使用match的方法