比较转换时间格式的时间

时间:2016-01-07 10:42:44

标签: javascript jquery

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

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('');
        }
    }
}

谢谢。

5 个答案:

答案 0 :(得分:2)

我希望你正在寻找这样的逻辑:

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');
	}
}

答案 1 :(得分:0)

试试这个例子..

//Set the extracted part of the time to variables. 
// If you dont have the second part then set it to 0.
var startHour = extractedStartHour;
var startMinute = extractedStartMinute;
var startSecond = extractedStartSecond;

var endHour = extractedEndHour;
var endMinute = extractedEndMinute;
var endSecond = extractedEndSecond;

//Create date object and set the time to that
var startTimeObject = new Date();
startTimeObject.setHours(startHour, startMinute, startSecond);

//Create date object and set the time to that
var endTimeObject = new Date(startTimeObject);
endTimeObject.setHours(endHour, endMinute, endSecond);

//Now we are ready to compare both the dates
if (startTimeObject > endTimeObject) {
    alert('End time should be after start time.');
} else {
    alert('Entries are perfect.');
}

答案 2 :(得分:0)

在两个时间前添加相同的静态日期,并使用以下编码

var d=new Date("October 16, 1990 10:30 AM");
var starttime = d.getTime();

var d=new Date("October 16, 1990 12:30 PM");
var endtime = d.getTime();

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('');
                }
            }

答案 3 :(得分:0)

为简单起见,只需转换为数字并为PM添加偏移量即可。 replace(/\D/g,'')将替换所有非数字字符。

var startTime = $('#start_time').val();
var endTime = $('#end_time').val();

var startTimeValue = parseInt(startTime.replace(/\D/g,''));
var endTimeValue = parseInt(endTime.replace(/\D/g,''));

startTimeValue += startTime.indexOf("PM") > -1 ? 1200 : 0;
endTimeValue += endTime.indexOf("PM") > -1 ? 1200 : 0;

alert(startTimeValue <= endTimeValue);

答案 4 :(得分:0)

试试吧

.enquiry_fld_normal{
width:278px !important; 
}

.enquiry_fld_normal_select{
width:280px !important; 
 }