开发者

Compare 2 dates in format DD/MM/YYYY with javascript/jquery

Suppose I receive two dates from the datepicker plugin in format DD/MM/YYYY

var date1 = '25/02/1985';  /*february 25th*/
var date2 = '26/02/1985';  /*february 26th*/开发者_JAVA技巧
/*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;
}


Split on the "/" and use the Date constructor.

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


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 this out. Thanks.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜