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

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

moment().duration().add() 方法用于将给定的时间添加到持续时间中。要添加的时间可以是另一个 Duration 对象,以毫秒为单位的值,或者使用字符串和数字值表示的时间。创建持续时间时使用的所有简写和键都可以在此处用于时间。

语法:

moment().duration().add(Number) // or
moment().duration().add(Duration) // or
moment().duration().add(Object) // or
moment().duration().add(Number, String)

参数:

该方法接受一个参数,表示要添加的时间。可用的格式包括:

  • Number: 表示将要添加的时间的毫秒数。
  • Duration: 表示将要添加的另一个 Duration 对象。
  • Object: 表示要添加的时间作为一个对象。
  • Number, String: 使用数字值和字符串值作为其类型表示要添加的时间。

返回值:

返回修改后的 Duration 对象。

注意:

在普通的 Node.js 程序中无法使用此方法,因为它需要全局或项目目录中安装外部的 moment.js 库。

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

moment 模块的安装:

npm install moment

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

示例1:

const moment = require('moment'); 
  
let durationOne = moment.duration(9, 'months'); 
console.log( 
    "Data of durationOne before adding:", 
    durationOne._data 
) 
console.log( 
    "Humanized Data of durationOne before adding:", 
    durationOne.humanize() 
) 
  
// Adding using add(Number, String) 
durationOne.add(600, 'days'); 
console.log( 
    "Data of durationOne after adding:", 
    durationOne._data 
) 
console.log( 
    "Humanized Data of durationOne after adding:", 
    durationOne.humanize() 
) 
  
let durationTwo = moment.duration(5, 'seconds'); 
console.log( 
    "Data of durationTwo before adding:",  
    durationTwo._data 
) 
console.log( 
    "Humanized Data of durationTwo before adding:", 
    durationTwo.humanize() 
) 
  
// Adding using add(Number) 
durationTwo.add(2050); 
console.log( 
    "Data of durationTwo after adding:", 
    durationTwo._data 
) 
console.log( 
    "Humanized Data of durationTwo after adding:", 
    durationTwo.humanize() 
)

输出:

 Data of durationOne before adding: {
  milliseconds: 0,
  seconds: 0,
  minutes: 0,
  hours: 0,
  days: 0,
  months: 9,
  years: 0
}
Humanized Data of durationOne before adding: 9 months
Data of durationOne after adding: {
  milliseconds: 0,
  seconds: 0,
  minutes: 0,
  hours: 0,
  days: 21,
  months: 4,
  years: 2
}
Humanized Data of durationOne after adding: 2 years
Data of durationTwo before adding: {
  milliseconds: 0,
  seconds: 5,
  minutes: 0,
  hours: 0,
  days: 0,
  months: 0,
  years: 0
}
Humanized Data of durationTwo before adding: a few seconds
Data of durationTwo after adding: {
  milliseconds: 50,
  seconds: 7,
  minutes: 0,
  hours: 0,
  days: 0,
  months: 0,
  years: 0
}
Humanized Data of durationTwo after adding: a few seconds

示例2:

const moment = require('moment'); 
  
let durationThree = moment.duration(15, 'hours'); 
console.log( 
    "Data of durationThree before adding:", 
    durationThree._data 
) 
console.log( 
    "Humanized Data of durationThree before adding:", 
    durationThree.humanize() 
) 
  
let durationToAdd = moment.duration(5, 'days'); 
  
// Adding using add(Duration) 
durationThree.add(durationToAdd); 
console.log( 
    "Data of durationThree after adding:", 
    durationThree._data 
) 
console.log( 
    "Humanized Data of durationThree after adding:", 
    durationThree.humanize() 
) 
  
let durationFour = moment.duration(4, 'hours'); 
console.log( 
    "Data of durationFour before adding:", 
    durationFour._data 
) 
console.log( 
    "Humanized Data of durationFour before adding:", 
    durationFour.humanize() 
) 
  
// Adding using add(Object) 
let durationToAddAgain = moment.duration({months: 2, days: 16}); 
  
durationFour.add(durationToAddAgain); 
console.log( 
    "Data of durationFour after adding:", 
    durationFour._data 
) 
console.log( 
    "Humanized Data of durationFour after adding:", 
    durationFour.humanize() 
)

输出:

Data of durationThree before adding: {
  milliseconds: 0,
  seconds: 0,
  minutes: 0,
  hours: 15,
  days: 0,
  months: 0,
  years: 0
}
Humanized Data of durationThree before adding: 15 hours
Data of durationThree after adding: {
  milliseconds: 0,
  seconds: 0,
  minutes: 0,
  hours: 15,
  days: 5,
  months: 0,
  years: 0
}
Humanized Data of durationThree after adding: 6 days
Data of durationFour before adding: {
  milliseconds: 0,
  seconds: 0,
  minutes: 0,
  hours: 4,
  days: 0,
  months: 0,
  years: 0
}
Humanized Data of durationFour before adding: 4 hours
Data of durationFour after adding: {
  milliseconds: 0,
  seconds: 0,
  minutes: 0,
  hours: 4,
  days: 16,
  months: 2,
  years: 0
}
Humanized Data of durationFour after adding: 3 months

参考:

https://momentjs.com/docs/#/durations/add/

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程