Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

moment.js

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

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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
Using the isAfter() method in Moment.js to compare times

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.

RELATED TAGS

moment.js

CONTRIBUTOR

Theodore Kelechukwu Onyejiaku

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring