Moment.js moment().startOf() Method
The moment().startOf() method is used to mutate the Moment so that it is set to the start 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().startOf( String );
Parameters: This method accepts a single parameter as mentioned above and described below:
- String: It is the unit of time from which the Moment object will be set to begin from.
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().startOf() Method.
Example 1:
Javascript
const moment = require( 'moment' ); console.log( "Current moment is:" , moment().toString()); // This will set the moment to 12:00 am today console.log( "startOf current moment's day:" , moment().startOf('day ').toString() ) // This will set the moment to the // first day and hour of this week console.log( "startOf current moment' s week:", moment().startOf( 'week' ).toString() ) // This will set the moment to the // first day and hour of this month console.log( "startOf current moment's month:" , moment().startOf('month ').toString() ) // This will set the moment to the // beginning of the current quarter // 1st day and hour of month console.log( "startOf current moment' s quarter:", moment().startOf( 'quarter' ).toString() ) // This will set the moment to the // first day and hour of the year console.log( "startOf current moment's year:" , moment().startOf('year').toString() ) |
Output:
Current moment is: Mon Jul 18 2022 02:33:56 GMT+0530
startOf current moment’s day: Mon Jul 18 2022 00:00:00 GMT+0530
startOf current moment’s week: Sun Jul 17 2022 00:00:00 GMT+0530
startOf current moment’s month: Fri Jul 01 2022 00:00:00 GMT+0530
startOf current moment’s quarter: Fri Jul 01 2022 00:00:00 GMT+0530
startOf current moment’s year: Sat Jan 01 2022 00:00:00 GMT+0530
Example 2:
Javascript
const moment = require( 'moment' ); console.log( "Current moment is:" , moment().toString()); // This will set the moment to the // first millisecond of the second console.log( "startOf current moment's second:" , moment().startOf('second ').toString() ) // This will set the moment to the // first second of the minute console.log( "startOf current moment' s minute:", moment().startOf( 'minute' ).toString() ) // This will set the moment to the // first minute of the hour console.log( "startOf current moment's hour:" , moment().startOf('hour').toString() ) |
Output:
Current moment is: Mon Jul 18 2022 02:33:56 GMT+0530
startOf current moment’s second: Mon Jul 18 2022 02:33:56 GMT+0530
startOf current moment’s minute: Mon Jul 18 2022 02:33:00 GMT+0530
startOf current moment’s hour: Mon Jul 18 2022 02:00:00 GMT+0530
Reference: https://momentjs.com/docs/#/manipulating/start-of/