📜  moment.add - Javascript (1)

📅  最后修改于: 2023-12-03 15:17:41.050000             🧑  作者: Mango

moment.add() - Javascript

moment.add() is a method in the moment.js library that allows you to add time to a given moment object. This method is very useful for time calculations in Javascript.

Syntax
moment.add(Number, String);

The first parameter represents the amount of time to add, while the second parameter represents the unit of time.

Parameters
  • Number - specifies the amount of time to add.
  • String - specifies the unit of time to add. This can be either "milliseconds", "seconds", "minutes", "hours", "days", "weeks", "months", or "years".
Return Value

The moment.add() method returns a new moment object with the added time.

Example
// Add 1 day to the current date
var today = moment();
var tomorrow = today.add(1, 'days');

// Add 1 hour to the current time
var now = moment();
var later = now.add(1, 'hours');

// Add 2 weeks to a specific date
var specificDate = moment('2021-10-01');
var futureDate = specificDate.add(2, 'weeks');
Conclusion

moment.add() is a very useful method in the moment.js library that allows you to easily add time to a moment object. This method can be used for a wide range of time calculations in your Javascript projects.