Moment JS Check if Date is Future or not Example

Jquery

Nicesnippets

226

01-05-2021


Moment JS Check if Date is Future or not Example

Hello Friends,

Now let's see example of how to check if date is future or not example using moment js. I will learn you moment js check if date is future or not example. We will talk about how to check if date is future or not example in moment js.

In this blog, i will give you simple example of jquery moment js check given date is future or not using diff() and isAfter() method provide you last next month date.

Here i will give two example for how to check if date is future or not example using moment js. So let's see the bellow example:

Example 1


<!DOCTYPE html>

<html>

<head>

<title>jquery moment example - NiceSnippets.com</title>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" crossorigin="anonymous"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" crossorigin="anonymous"></script>

</head>

<body>

<h1>jquery moment example - NiceSnippets.com</h1>

</body>

<script type="text/javascript">

var oneDate = moment('02-01-2021', 'DD-MM-YYYY');

var twoDate = moment('12-01-2021', 'DD-MM-YYYY');

var dDiff = oneDate.diff(twoDate);

if (dDiff < 0) {

console.log('Date is future date');

}else{

console.log('Date is not future date');

}

</script>

</html>

Output:

Date is future date

Example 2

<!DOCTYPE html>

<html>

<head>

<title>jquery moment example - NiceSnippets.com</title>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" crossorigin="anonymous"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" crossorigin="anonymous"></script>

</head>

<body>

<h1>jquery moment example - NiceSnippets.com</h1>

</body>

<script type="text/javascript">

var todayDate = moment();

var futureDate = moment('12-01-2021', 'DD-MM-YYYY');

if (!todayDate.isAfter(futureDate)) {

console.log('Date is future');

}else{

console.log('Date is not future');

}

</script>

</html>

Output:

Date is future date

It will help you....


Recommended Posts