你可以分析你的
String date
到
Date Object
并进行比较:
function CompareDate(dateStr1,dateStr2) {
var dateArry1 = dateStr1.split(".");
var dateArry2 = dateStr2.split(".");
//JavaScript counts months from 0 index so we have to do -1:January - 0, February - 1, and so on.....
var dateOne = new Date(dateArry1[2], dateArry1[1]-1, dateArry1[0]); //Year, Month, Date
var dateTwo = new Date(dateArry2[2], dateArry2[1]-1, dateArry2[0]); //Year, Month, Date
if (dateOne > dateTwo) {
console.log("Date One is greather then Date Two.");
return true;
}else if(dateOne < dateTwo) {
console.log("Date Two is greather then Date One.");
return false;
}else if(dateOne.toDateString() === dateTwo.toDateString()) {
console.log("Date are same.");
return false;
}
return false;
}
console.log(CompareDate("23.3.2018","24.3.2018"));
//VM16380:1 false
//undefined
console.log(CompareDate("23.3.2018","24.3.2018"));
//VM16381:1 true
//undefined
console.log(CompareDate("24.3.2017", "24.3.2018"));
//VM16384:1 true
//undefined
console.log(CompareDate("24.03.2018" , "20.3.2017"));
console.log(CompareDate("24.3.2018" , "24.03.2018"));
//VM16385:1 false
警告!
在某些浏览器中,没有前导零的月份或天数可能会产生错误:
var d = new Date("2015-3-25");
所以最好准备一下
zero
如果长度为
1
.