How to compare date in JavaScript [duplicate]

。_饼干妹妹 提交于 2019-12-11 18:55:08

问题


I want to compare following to dates i.e. d1 with d2:

var d1 = new Date(12,05,2013);
var d2 = "12/05/2013";

回答1:


convert date to timestamp

DateObject.getTime(); will give timestamp

and convert string to date new Date(d2)

javScript

var d1 = new Date("12/05/2013");
var d2 = "12/05/2013";
console.log(d1.getTime());
console.log(new Date(d2).getTime());

if(d1.getTime() == new Date(d2).getTime()){
   //do something
}



回答2:


To compare two variables you need to use JS operators, consider:

if(d1==d2)
{
 //do this
}

else
{
 //do this
}

More information on logical operators can be found here: http://www.w3schools.com/js/js_comparisons.asp



来源:https://stackoverflow.com/questions/18697888/how-to-compare-date-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!