Home > Web Front-end > JS Tutorial > body text

How to implement date comparison in JS

醉折花枝作酒筹
Release: 2021-04-29 09:21:02
forward
7440 people have browsed it

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”格式
Copy after login

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

[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!

Related labels:
js
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!