Open In App

Moment.js moment().set() Method

Last Updated : 26 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The moment().set() method is used to set the given unit of time to the Moment object. The unit can be specified in all the recognized variations of the unit including its plural and short forms. The time can also be set using an object that contains all the needed units of time together.

Syntax:

moment().set(String, Int)

OR

moment().set(Object(String, Int))

Parameters: This method accepts two parameters as mentioned above and described below:

  • String: It is the unit of time that has to be set for the Moment object.
  • Int: It is the time value that has to be set.
  • Object: The time can also be specified as a JSON object with all the units of time and their value.

Return Value: This method returns a string of the given unit of time from the Moment object.

Note: This will not work in the normal Node.js program because it requires an external moment.js library to be installed globally or in the project directory.

Moment.js can be installed using the following command:

Installation of moment module:

npm install moment

The below examples will demonstrate the Moment.js moment().set() Method.

Example 1:

Javascript




const moment = require('moment');
  
let momentOne = moment();
  
momentOne.set('year', 2010);
momentOne.set('month', 6);
momentOne.set('date', 10);
  
console.log("MomentOne is:", momentOne.toString());
console.log("MomentOne year:", momentOne.year());
console.log("MomentOne month:", momentOne.month());
console.log("MomentOne date:", momentOne.date());
  
let momentTwo = moment();
  
momentTwo.set('y', 2022);
momentTwo.set('m', 8);
momentTwo.set('d', 19);
  
console.log("MomentTwo is:", momentTwo.toString());
console.log("MomentTwo year:", momentTwo.year());
console.log("MomentTwo month:", momentTwo.month());
console.log("MomentTwo date:", momentTwo.date());


Output:

MomentOne is: Sat Jul 10 2010 00:28:08 GMT+0530
MomentOne year: 2010
MomentOne month: 6
MomentOne date: 10
MomentTwo is: Fri Aug 12 2022 00:08:08 GMT+0530
MomentTwo year: 2022
MomentTwo month: 7
MomentTwo date: 12

Example 2:

Javascript




const moment = require('moment');
  
let moment1 = moment();
  
moment1.set('hour', 10);
moment1.set('minute', 18);
moment1.set('second', 30);
moment1.set('millisecond', 150);
  
console.log(
    "moment1 is:",
    moment1.toString()
);
console.log(
    "moment1 hour:",
    moment1.hour()
);
console.log(
    "moment1 minute:",
    moment1.minute()
);
console.log(
    "moment1 second:",
    moment1.second()
);
console.log(
    "moment1 millisecond:",
    moment1.millisecond()
);
  
let moment2 = moment();
  
moment2.set('hour', 6);
moment2.set('minute', 30);
moment2.set('second', 10);
moment2.set('millisecond', 3500);
  
console.log(
    "moment2 is:",
    moment2.toString()
);
console.log(
    "moment2 hour:",
    moment2.hour()
);
console.log(
    "moment2 minute:",
    moment2.minute()
);
console.log(
    "moment2 second:",
    moment2.second()
);
console.log(
    "moment2 millisecond:",
    moment2.millisecond()
);


Output:

moment1 is: Sun Jul 24 2022 10:18:30 GMT+0530
moment1 hour: 10
moment1 minute: 18
moment1 second: 30
moment1 millisecond: 150
moment2 is: Sun Jul 24 2022 06:30:13 GMT+0530
moment2 hour: 6
moment2 minute: 30
moment2 second: 13
moment2 millisecond: 500

Reference: https://momentjs.com/docs/#/get-set/set/



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads