The Wayback Machine - https://web.archive.org/web/20240219125109/https://www.geeksforgeeks.org/moment-js-moment-hour-method/
Open In App
Related Articles

Moment.js moment.hour() Method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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/

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!


Last Updated : 12 Mar, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials