将 DD/MM/YYYY 格式的 2 个日期与 javascript/jquery 进行比较
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7335075/
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
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:51:17 来源:igfitidea点击:
Compare 2 dates in format DD/MM/YYYY with javascript/jquery
提问by Toni Michel Caubet
Suppose I receive two dates from the datepicker plugin in format DD/MM/YYYY
假设我从 datepicker 插件中收到两个格式的日期 DD/MM/YYYY
var date1 = '25/02/1985'; /*february 25th*/
var date2 = '26/02/1985'; /*february 26th*/
/*this dates are results form datepicker*/
if(process(date2) > process(date1)){
alert(date2 + 'is later than ' + date1);
}
What should this function look like?
这个函数应该是什么样的?
function process(date){
var date;
// Do something
return date;
}
回答by InvisibleBacon
Split on the "/" and use the Date constructor.
在“/”上拆分并使用Date 构造函数。
function process(date){
var parts = date.split("/");
return new Date(parts[2], parts[1] - 1, parts[0]);
}
回答by rony36
It could be more easier:
可能更容易:
var date1 = '25/02/1985'; /*february 25th*/
var date2 = '26/02/1985'; /*february 26th*/
if ($.datepicker.parseDate('dd/mm/yy', date2) > $.datepicker.parseDate('dd/mm/yy', date1)) {
alert(date2 + 'is later than ' + date1);
}
For more details check thisout. Thanks.
有关更多详细信息,请查看此内容。谢谢。
回答by Maxx
function process(date){
var parts = date.split("/");
var date = new Date(parts[1] + "/" + parts[0] + "/" + parts[2]);
return date.getTime();
}