This article will introduce to you how to implement date comparison in JS. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
Method one:
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(“2019-05-01”,“2019-05-05”);//须将日期转换成“YYYY-MM-DD”格式
Method two:
var myDate=new Date(); myDate.setFullYear(2008,8,9); var today = new Date(); if (myDate>today) { alert("Today is before 9th August 2008"); } else { alert("Today is after 9th August 2008"); }
[Recommended learning: javascript advanced tutorial]
The above is the detailed content of How to implement date comparison in JS. For more information, please follow other related articles on the PHP Chinese website!