Javascript 将开始时间和结束时间与 jquery 进行比较?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11244943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Compare start time amd end time with jquery?
提问by Frank
Here the code
这里的代码
$(document).ready(function() {
$('#st').change(function(){
var st = $('#st').val(); // start time Format: '9:00 PM'
var et = $('#et').val(); // end time Format: '11:00 AM'
//how do i compare time
if(st > et)
{
alert('end time always greater then start time');
}
});
});
if i have time range as follows
如果我有如下时间范围
Start Time Range (listbox) = 12:00 AM To 11:59PM
End Time Range (listbox) = 12:00 AM To 11:59PM
then how to validate start-time less then end-time OR end-time greater then start-time
那么如何验证开始时间小于结束时间或结束时间大于开始时间
Start-time < End-time OR End-time > St-time
consider time format in js-code, due to time-format am unable to implement difference logic
在js-code中考虑时间格式,由于时间格式无法实现差异逻辑
Format: '9:00 AM'
Format: '5:00 PM'
回答by Frank
Here i got after investigate this.
The best practice if Time Formatin 12 Hour-TimeOr 24 Hour-Time.
这是我调查后得到的。
如果时间格式为12 小时制或24 小时制,最佳实践。
//start time
var start_time = $("#start_time").val();
//end time
var end_time = $("#end_time").val();
//convert both time into timestamp
var stt = new Date("November 13, 2013 " + start_time);
stt = stt.getTime();
var endt = new Date("November 13, 2013 " + end_time);
endt = endt.getTime();
//by this you can see time stamp value in console via firebug
console.log("Time1: "+ stt + " Time2: " + endt);
if(stt > endt) {
$("#start_time").after('<span class="error"><br>Start-time must be smaller then End-time.</span>');
$("#end_time").after('<span class="error"><br>End-time must be bigger then Start-time.</span>');
return false;
}
回答by pimvdb
You have to convert strings to values you can compare in terms of time. Date.parse
comes in handy, though you're required to pass a date as well. Since you only care about time, use the same fake date for both.
您必须将字符串转换为可以在时间方面进行比较的值。Date.parse
派上用场,虽然你也需要通过一个日期。由于您只关心时间,因此对两者使用相同的假日期。
if(Date.parse("1-1-2000 " + st) > Date.parse("1-1-2000 " + et)) {
回答by Shruti Sehgal
You can even try the following jquery rule : jQuery.validator.addMethod
您甚至可以尝试以下 jquery 规则:jQuery.validator.addMethod
jQuery.validator.addMethod("timeValidator",
function (value, element, params) {
var val = new Date('1/1/1991' + ' ' + value);
var par = new Date('1/1/1991' + ' ' + $(params).val());
if (!/Invalid|NaN/.test(new Date(val))) {
return new Date(val) > new Date(par);
}
return isNaN(val) && isNaN(par)
|| (Number(val) > Number(par));
}, 'End Time must be greater than Start Time.');
You can call the rule :
您可以调用规则:
$("#txtToTime").rules('add', { timeValidator: "#txtFromTime" });
回答by epascarello
If they are select elements and the order in the select elements is the same, compare the selectedIndex. No need to parse dates.
如果是select元素并且select元素中的顺序相同,则比较selectedIndex。无需解析日期。
回答by SpacedMonkey
Turn them into Date objects:
将它们转换为 Date 对象:
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date
Then use the usual comparators:
然后使用通常的比较器: