What is the isAfter() method in Moment.js?

Overview

The isAfter() method of the Moment.js time library is used to check if one time occurs later than another time. It returns a boolean that indicates if one time comes after another.

Syntax

time1.isAfter(time2)
The isAfter() method of Moment.js

Parameters

  • time1 and time2: These are the times we want to compare. We compare time2 against time1.

Return value

The returned value is a boolean.

Example

// require the moment module
const moment = require("moment")
// create some dates and time
const time1 = moment(new Date) // current date and time
const time2 = moment([2010, 1, 14, 10, 25, 50, 125])
const time3 = moment(1318781876406)
const time4 = moment({hour: 20, minute: 20, second: 34})
const time5 = moment([2022, 5, 17])
const time6 = moment([2025, 4, 18])
const time7 = moment().add(25, "seconds");
// get the difference between one time and another
console.log(time1.isAfter(time2)) // true
console.log(time2.isAfter(time3)) // false
console.log(time3.isAfter(time4)) // false
console.log(time4.isAfter(time5)) // false
console.log(time6.isAfter(time5)) // true
console.log(time7.isAfter(time2)) // true

Explanation

  • Line 2: We require the moment library.
  • Line 5–11: We create some time variables using the library.
  • Line 14–19: Using the isAfter() method, we compare the times and print the results to the console.

Free Resources