Home  >  Article  >  Web Front-end  >  How to implement date comparison in JS

How to implement date comparison in JS

醉折花枝作酒筹
醉折花枝作酒筹forward
2021-04-29 09:21:027406browse

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.

How to implement date comparison in JS

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!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete