开发者

JS - Compare which date is older

I want to compare two dates as which is bigger in those dates.

var date1 = 2011-9-2;
var date1 = 2011-17-06;

Can anybody say how can I do 开发者_开发知识库this?


You'll need to convert both strings to date objects first.

var date1 = new Date('2011-09-02');//yyyy-mm-dd format
var date2 = new Date('2011-06-17');
if(date1 > date2){
  alert('date1 is bigger than date2'); 
}

Once you have the 2 variables as date objects you can compare them against each other (without needing to convert to milliseconds/minutes/?)


Check this link

And then do something like this:

var days = 0;
var difference = 0;
Christmas = new Date("December 25, 2005");
today = new Date();
difference = Christmas - today;
days = Math.round(difference/(1000*60*60*24));

Code source


Create Date objects from your two values (check this link) and use that to do the comparison.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜