Moment.js moment.duration().get(String) 方法

Moment.js moment.duration().get(String) 方法

moment().duration().get()方法 用于从持续时间对象中返回指定的时间值。它基本上是其他获取器函数(如weeks()、months()、hours()等)的替代方法。它还支持来自moment.add()方法的所有简写键。

语法:

moment().duration().get(String);

参数: 此方法接受一个单一参数,如上所述,并如下所述进行描述:

  • String: 此参数用于指定要返回的时间单位。

返回值: 此方法从持续时间中返回请求的时间单位。对于所有值,无效的持续时间将返回 NaN。

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

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

安装 moment 模块:

npm install moment

下面的示例将演示 Moment.js moment.duration().get()方法

示例1:

const moment = require('moment'); 
  
let durationOne =  
  moment.duration(2, 'days'); 
let durationTwo =  
  moment.duration(5, 'years'); 
let durationThree =  
  moment.duration(450, 'seconds'); 
  
// This is the same as .days() method 
console.log( 
  "durationOne get days is:", 
  durationOne.get('days') 
) 
  
// This will return 0 as the number of months 
// after a full year is considered is 0 
// This is the same as .months() method 
console.log( 
  "durationTwo get months is:", 
  durationTwo.get('months') 
) 
  
// This will return 7 as the number of minutes 
// of the duration is 7 
// This is the same as .minutes() method 
console.log( 
  "durationThree get minutes is:", 
  durationThree.get('minutes') 
) 
  
// This will return 30 as the number of seconds 
// after 7 full minutes is 30 
// This is the same as .seconds() method 
console.log( 
  "durationThree get seconds is:", 
  durationThree.get('seconds') 
)

输出:

durationOne get days is: 2
durationTwo get months is: 0
durationThree get minutes is: 7       
durationThree get seconds is: 30

示例2:

const moment = require('moment'); 
  
let durationA = 
  moment.duration({months: 4, days: 5, hours: 9}); 
let durationB =  
  moment.duration({hours: 73, minutes: 31}); 
  
console.log( 
  "durationA get number of hours is:", 
  durationA.get('hours') 
) 
console.log( 
  "durationA get number of days is:", 
  durationA.get('days') 
) 
console.log( 
  "durationA get number of months is:", 
  durationA.get('months') 
) 
  
console.log( 
  "durationB get number of hours is:", 
  durationB.get('hours') 
) 
console.log( 
  "durationB get number of minutes is:", 
  durationB.get('minutes'
))

输出:

durationA get number of hours is: 9
durationA get number of days is: 5
durationA get number of months is: 4
durationB get number of hours is: 1
durationB get number of minutes is: 31

示例3: 在这个示例中,我们将看一些可用的快捷键。

const moment = require('moment'); 
  
let duration1 =  
  moment.duration({months: 4, days: 5, hours: 9}); 
let duration2 = 
  moment.duration({hours: 73, minutes: 31}); 
  
console.log( 
  "duration1 get number of hours is:", 
  duration1.get('h') 
) 
console.log( 
  "duration1 get number of days is:", 
  duration1.get('d') 
) 
console.log( 
  "duration1 get number of months is:", 
  duration1.get('M') 
) 
  
console.log( 
  "duration2 get number of hours is:", 
  duration2.get('h') 
) 
console.log( 
  "duration2 get number of minutes is:", 
  duration2.get('m') 
 )

输出:

duration1 get number of hours is: 9
duration1 get number of days is: 5
duration1 get number of months is: 4
duration2 get number of hours is: 1
duration2 get number of minutes is: 31

参考: https://momentjs.com/docs/#/durations/get/

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程