JS实现日期比较大小(JavaScript)

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(“2019-05-01”,“2019-05-05”);//须将日期转换成“YYYY-MM-DD”格式





方法二:

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");
}

你可能感兴趣的:(JS)