JS代码中经常需要做日期大小比较:
这边做个总结:
方法一:
function compareDate(dateTime1,dateTime2){var formatDate1 = new Date(dateTime1);var formatDate2 = new Date(dateTime2);if(formatDate1 > formatDate2){return formatDate1;}else{return formatDate2;}}测试代码:var date = compareDate(“-05-01”,“-05-05”);//须将日期转换成“YYYY-MM-DD”格式
方法二:
var myDate=new Date();myDate.setFullYear(,8,9);var today = new Date();if (myDate>today){alert("Today is before 9th August ");}else{alert("Today is after 9th August ");}
如果觉得《JS实现日期比较大小(JavaScript)》对你有帮助,请点赞、收藏,并留下你的观点哦!