Open In App

Moment.js moment().endOf() Method

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

The moment().endOf() method is used to mutate the Moment so that it is set to the end of the given unit of time. The available units of time can be the year, month, quarter, week, day, hour, minute and second.

Syntax:

moment().endOf( String );

Parameters: This method accepts a single parameter as mentioned above and described below:

  • String: It is the unit of time for which the Moment object will be set to end.

Return Value: This method returns the mutated Moment after the value has been set.

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().endOf() Method.

Example 1:

Javascript




const moment = require('moment');
  
console.log("Current moment is:", moment().toString());
  
console.log(
    "endOf current moment's day:",
    moment().endOf('day').toString()
)
  
console.log(
    "endOf current moment's week:",
    moment().endOf('week').toString()
)
  
console.log(
    "endOf current moment's month:",
    moment().endOf('month').toString()
)
  
console.log(
    "endOf current moment's quarter:",
    moment().endOf('quarter').toString()
)
  
console.log(
    "endOf current moment's year:",
    moment().endOf('year').toString()
)


Output:

Current moment is: Mon Jul 18 2022 02:36:03 GMT+0530
endOf current moment’s day: Mon Jul 18 2022 23:59:59 GMT+0530
endOf current moment’s week: Sat Jul 23 2022 23:59:59 GMT+0530
endOf current moment’s month: Sun Jul 31 2022 23:59:59 GMT+0530
endOf current moment’s quarter: Fri Sep 30 2022 23:59:59 GMT+0530
endOf current moment’s year: Sat Dec 31 2022 23:59:59 GMT+0530

Example 2:

Javascript




const moment = require('moment');
  
console.log("Current moment is:",
    moment().toString());
  
console.log(
    "endOf current moment's second:",
    moment().endOf('second').toString()
)
  
console.log(
    "endOf current moment's minute:",
    moment().endOf('minute').toString()
)
  
console.log(
    "endOf current moment's hour:",
    moment().endOf('hour').toString()
)


Output:

Current moment is: Mon Jul 18 2022 02:36:03 GMT+0530
endOf current moment’s second: Mon Jul 18 2022 02:36:03 GMT+0530
endOf current moment’s minute: Mon Jul 18 2022 02:36:59 GMT+0530
endOf current moment’s hour: Mon Jul 18 2022 02:59:59 GMT+0530

Reference: https://momentjs.com/docs/#/manipulating/end-of/



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads