Open In App

Moment.js moment.duration().seconds() Method

Improve
Improve
Like Article
Like
Save
Share
Report

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');
  
// This returns 50 as the number of
// seconds is less than a whole minute (60 seconds)
console.log(
    "durationOne seconds is:", durationOne.seconds()
)
  
// This returns 8 as the duration has
// 6 complete minutes (360 seconds), 
// and therefore returns the value of
// seconds of the next minute (next 8 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');
  
// The asSeconds() method will return the
// length of the duration in seconds
console.log(
    "Length of durationA in seconds is:", durationA.asSeconds()
)
  
// It returns the number of complete seconds
console.log("durationA seconds is:", durationA.seconds())
  
console.log(
    "Length of durationB in seconds is:", durationB.asSeconds()
)
  
// It returns the number of complete seconds
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
Previous
Next
Share your thoughts in the comments
Similar Reads