Moment.js moment.duration().seconds() 方法

Moment.js moment.duration().seconds() 方法

这个 moment().duration().seconds() 方法用于获取持续时间的秒数。这个秒数是作为分钟的一部分计算出来的,因此它的值在 0 和 59 之间。

语法:

moment().duration().seconds();

参数: 此方法不接受任何参数。

返回值: 此方法返回持续时间的秒数(0-59)。

注意: 在普通的Node.js程序中,这个方法不起作用,因为它需要在全局范围或项目目录中安装外部的moment.js库。

可以使用以下命令安装Moment.js:

安装moment模块:

npm install moment

以下示例将演示Moment.js moment.duration().seconds() 方法。

示例1:

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() 
)

输出:

durationOne seconds is: 50
durationTwo seconds is: 8

示例2: 这个示例将帮助理解这种方法与asSeconds()之间的区别,以便更好地理解。

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())

输出:

Length of durationA in seconds is: 36.5
durationA seconds is: 36
Length of durationB in seconds is: 3.858
durationB seconds is: 3

参考资料: https://momentjs.com/docs/#/durations/seconds/

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程