Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

time
moment.js
javascript
add
communitycreator

What is the add() 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 add() method in moment.js adds time to a particular time. We can add more days, more hours, more weeks, more months, and so on.

Syntax

Here is the syntax for the add() method:

time.add(number, shorthand)
The syntax for add() method in moment.js

Parameters

time: This is the time before adding the additional time.

number: This represents the number of the time unit we want to add. It could be 7, 3, or more.

string: This represents the kind of time we want to add. It could be months, seconds, and so on. For example, we can use "y" or years, "M" or months, and so on.

Return value

It returns a new time with more time added to it.

Example

// require the moment module
const moment = require("moment")

// create some dates and time
const time1 = moment([2010, 1, 14, 10, 25, 50, 125])
const time2 = moment(new Date)  // current date and time
const time3 = moment(1318781876406)
const time4 = moment({hour: 20, minute: 20, second: 34})

console.log("before adding more time")
// get the hour of the time
console.log(time1.format("DD MM YYYY hh:mm:ss")) // 10 
console.log(time2.format("DD MM YYYY hh:mm:ss")) // 15
console.log(time3.format("DD MM YYYY hh:mm:ss")) // 16
console.log(time4.format("DD MM YYYY hh:mm:ss")) // 20

// add more time
time1.add(2, "days")
time2.add(20, "h")
time3.add(12, "minutes")
time4.add(4, "Y")

console.log("After adding more hour")
// get the hour of the time again
console.log(time1.format("DD MM YYYY hh:mm:ss")) 
console.log(time2.format("DD MM YYYY hh:mm:ss")) 
console.log(time3.format("DD MM YYYY hh:mm:ss")) 
console.log(time4.format("DD MM YYYY hh:mm:ss"))
The add() method in moment.js

Explanation

  • Line 2: We require the moment.js package.
  • Lines 5–8: We create some times using the package.
  • Lines 12-15: We use the format() method to get the year, month, date, hour, minutes, and seconds of the times we created and print them to the console.
  • Lines 18–21: We use the add() method to add more time to the times we created.
  • Lines 25–28: We print the new times.

RELATED TAGS

time
moment.js
javascript
add
communitycreator

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