Open In App

Moment.js moment.second() Method

The moment().second() Method is used to get the seconds from the current time or to set the seconds.

Syntax:



moment().second();
// or
moment().seconds();

Parameters: This method accepts a single parameter number that you want to set seconds then you will require an additional parameter:

Return Value: This function returns the current time in seconds.



Note: This will not work in the normal Node.js program because it requires the moment.js library to be installed. 

Moment.js can be installed using the following command:

npm install moment

Example 1: Getting the current time in seconds.




// Acquiring the plugin
const moment = require('moment');
 
let sec = moment().second();
 
console.log("Current seconds Time:", sec);

Output:

Current seconds Time: 18

Example 2: Setting seconds of time.




// Acquiring the plugin
const moment = require('moment');
 
let sec = moment().second(55);
 
console.log("Seconds Time:", sec);

Output: we can see the seconds in time.

Seconds Time: Moment<2020-08-05T20:59:55+05:30>
Article Tags :