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
time1andtime2: These are the times we want to compare. We comparetime2againsttime1.
Return value
The returned value is a boolean.
Example
// require the moment moduleconst moment = require("moment")// create some dates and timeconst time1 = moment(new Date) // current date and timeconst 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 anotherconsole.log(time1.isAfter(time2)) // trueconsole.log(time2.isAfter(time3)) // falseconsole.log(time3.isAfter(time4)) // falseconsole.log(time4.isAfter(time5)) // falseconsole.log(time6.isAfter(time5)) // trueconsole.log(time7.isAfter(time2)) // true
Explanation
- Line 2: We
requirethemomentlibrary. - 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.