Moment.js moment.duration().seconds() Method
The moment().duration().seconds() method is used to get the number of seconds of the duration. This number of seconds is calculated as a subset of a minute, therefore having a value between 0 and 59.
Syntax:
moment().duration().seconds();
Parameters: This method does not accept any parameters.
Return Value: This method returns the seconds (0-59) of the duration.
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.duration().seconds() Method.
Example 1:
Javascript
const moment = require( 'moment' );
let durationOne = moment.duration(50, 'seconds' );
let durationTwo = moment.duration(368, 'seconds' );
console.log(
"durationOne seconds is:" , durationOne.seconds()
)
console.log(
"durationTwo seconds is:" , durationTwo.seconds()
)
|
Output:
durationOne seconds is: 50
durationTwo seconds is: 8
Example 2: This example will help to understand the difference of this method with asSeconds() for a better understanding.
Javascript
const moment = require( 'moment' );
let durationA =
moment.duration(36500, 'milliseconds' );
let durationB =
moment.duration(3858, 'milliseconds' );
console.log(
"Length of durationA in seconds is:" , durationA.asSeconds()
)
console.log( "durationA seconds is:" , durationA.seconds())
console.log(
"Length of durationB in seconds is:" , durationB.asSeconds()
)
console.log( "durationB seconds is:" , durationB.seconds())
|
Output:
Length of durationA in seconds is: 36.5
durationA seconds is: 36
Length of durationB in seconds is: 3.858
durationB seconds is: 3
Reference: https://momentjs.com/docs/#/durations/seconds/
Last Updated :
23 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...