Moment JS Check if Date is Future or not Example

By Hardik Savani January 7, 2021 Category : jQuery

This simple article demonstrates of moment check date is future date. you will learn how to check date is future in moment. Here you will learn jquery moment check date is future. We will look at example of moment.js check if date is future.

Here, 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.

let's see both example with output:

Example 1:

<!DOCTYPE html>

<html>

<head>

<title>jquery moment example - ItSolutionStuff.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 - ItSolutionStuff.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 - ItSolutionStuff.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 - ItSolutionStuff.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

I hope it can help you...

Tags :
Shares