比较DD / MM / YYYY格式的2个日期和javascript / jquery

时间:2011-09-07 13:55:32

标签: javascript date

假设我以DD/MM/YYYY

格式从datepicker插件收到两个日期
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);
}

这个功能应该是什么样的?

function process(date){
   var date;
   // Do something
   return date;
}

3 个答案:

答案 0 :(得分:25)

答案 1 :(得分:7)

function process(date){
   var parts = date.split("/");
   var date = new Date(parts[1] + "/" + parts[0] + "/" + parts[2]);
   return date.getTime();
}

答案 2 :(得分:6)

可能会更容易:

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

}

了解更多详情check this。感谢。