javascript – 比较转换时间格式的时间

我有两次不同的时间.现在我想比较这两次相同的日期.我想查看哪个时间更长.我怎样才能做到这一点.这是我的代码:

start_time = 10:30 AM
end_time = 12:30 PM

function modify_time(){
    var start_time = $('#start_time').val();
    var end_time = $('#end_time').val();
    if (start_time != '' && end_time != '') { 
        alert(start_time); alert(end_time);
        if (end_time <= start_time) {
            alert('select valid time');
            $('#start_time').val('');
            $('#end_time').val('');
        }
    }
}

谢谢.

最佳答案 我希望你在寻找这样的逻辑:

var start_time = "10:30 AM";
var end_time = "10:20 AM";

start_time = start_time.toLowerCase(); 
end_time = end_time.toLowerCase(); 

function getMin(timeStrin)
{
  var isPM = false;
  if(timeStrin.indexOf("pm") > -1)
     isPM = true;
    
  timeStrin = timeStrin.replace("am","");
  timeStrin = timeStrin.replace("pm","");
  var timeArr =  timeStrin.split(":");
  var hr = parseInt(timeArr[0],10);
  var min = parseInt(timeArr[1],10);
  if(isPM && hr>=1 && hr != 12)
    hr = hr+12;
  
  return (hr*60 + min);
    
}


if (start_time != '' && end_time != '') { 
	
  start_time = getMin(start_time);
  end_time = getMin(end_time);
  alert(start_time); 
	alert(end_time);
	if (end_time <= start_time) {
		alert('select valid time');
	}
}
点赞