Moment.js moment.hour() Method
The moment().hour() Method is used to get the hours from the current time or to set the hours.
Syntax:
moment().hour();
or
moment().hours();
Parameters: This method accepts a single parameter number that you want to set hours then you will require an additional parameter:
Number::It ranges from 0 to 23 and used to set hours in a variable.
Return Value: This function returns the current time in hours
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:
Installing module:
npm install moment
Example 1: Getting the current time in hours.
index.js
// Importing moment module const moment = require( 'moment' ); var a = moment().hours(); console.log( "hours is: " ,a); |
Run index.js file using below command:
node index.js
Output:
hours is: 22
Example 2: Setting hours of time.
index.js
Javascript
// Importing moment module const moment = require( 'moment' ); var a = moment().hours(20); console.log(a); |
Run index.js file using below command:
node index.js
Output:
Moment<2021-02-10T20:34:30+05:30>
Reference: https://momentjs.com/docs/#/get-set/hour/
Contact Us