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....
- How to set Date in setUTCHours() Method in JavaScript?
- How to Date sets UTC milliseconds in Javascript?
- How to Use push(), unshift(),pop() and shift() With Array in Javascript?
- How to Check if a Variable is a Number in Javascript?
- How To Format a Number Using Fixed - Point Notation in Javascript?
- How to Insert an Element into an Array in javaScript?
- How to remove duplicate elements from JavaScript Array?
- How to calculate years, months and days between two dates in javascript?