Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Moment.js isBefore() Function

  • Last Updated : 24 Jul, 2020

It is used to check whether a date is before a particular date in Node.js using the isBefore() function that checks if a moment is before another moment. The first argument will be parsed as a moment, if not already so.

Syntax:

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

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

Returns: True or False

Installation of moment module:

  1. You can visit the link to Install moment module. You can install this package by using this command.
    npm install moment
  2. After installing the moment module, you can check your moment version in command prompt using the command.
    npm version moment
  3. 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

Example 1: Filename: index.js




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

Steps to run the program:

  1. The project structure will look like this:
  2. Make sure you have installed moment module using the following command:
    npm install moment
  3. Run index.js file using below command:
    node index.js

    Output:

    true
    false
    

Example 2: Filename: index.js




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

Run index.js file using below command:

node index.js

Output:

true

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


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!