Open In App

Moment.js isAfter() Function

Improve
Improve
Like Article
Like
Save
Share
Report

You can check whether a date is after a particular date in Moment.js using the isAfter() function that checks if a moment is after another moment. The first argument will be parsed as a moment, if not already so. 

Syntax:

moment().isAfter(Moment|String|Number|Date|Array);
moment().isAfter(Moment|String|Number|Date|Array, String);

Parameter: It can be either Moment|String|Number|Date|Array. 

Returns: True or False Installation of moment module:

You can visit the link to the Install moment module. You can install this package by using this command.

npm install moment

After installing the moment module, you can check your moment version in the command prompt using the command.

npm version moment

After that, you can just create a folder and add a file, for example, index.js. To run this file you need to run the following command.

node index.js

Project Structure:

Example 1: Filename: index.js 

javascript




// Requiring module
const moment = require('moment');
 
let bool1 = moment('2010-10-20')
    .isAfter('2010-10-21'); //false
console.log(bool1);
 
let bool2 = moment('2019-10-20')
    .isAfter('2010-12-31', 'year'); //true
console.log(bool2);


Steps to run the program:

Make sure you have installed the moment module using the following command:

npm install moment

Run the index.js file using the below command:

node index.js

Output:

false
true

Example 2: Filename: index.js 

javascript




// Requiring module
const moment = require('moment');
 
function checkIsAfter(date1, date2) {
    return moment(date1).isAfter(date2);
}
 
let bool = checkIsAfter('2010-10-20', '2010-10-21');
console.log(bool);


Steps to run the program:

Run the index.js file using the below command:

node index.js

Output:

false

Reference: https://momentjs.com/docs/#/query/is-after/



Last Updated : 20 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads