Moment.js moment.duration().subtract()方法
moment().duration().subtract() 方法用于从持续时间中减去给定的时间。要减去的时间可以是另一个持续时间对象、毫秒值或使用字符串和数字值表示的时间。在此处可以使用用于创建持续时间的所有速记和键。
语法:
moment().duration().subtract(Number)
moment().duration().subtract(Duration)
moment().duration().subtract(Object)
moment().duration().subtract(Number, String)
参数: 该方法接受一个参数,表示要减去的时间。可用的格式包括:
- Number: 这将是表示要减去的时间的毫秒数。
- Duration: 这表示要减去的另一个持续时间对象。
- Object: 这表示要减去的时间作为一个对象。
- Number, String: 这表示要减去的时间,使用数字值和其类型的字符串值表示。
返回值: 它返回修改过的持续时间对象。
注意: 这在普通的Node.js程序中不起作用,因为它需要全局安装或项目目录中安装外部的moment.js库。
可以通过以下命令安装moment.js:
moment模块的安装:
npm install moment
示例1: 以下示例将演示Moment.js moment.duration().subtract() 方法。
const moment = require('moment');
// Example 1
let durationOne = moment.duration(4, 'years');
console.log(
"Data of durationOne before subtracting:",
durationOne._data
)
console.log(
"Humanized Data of durationOne before subtracting:",
durationOne.humanize()
)
// Subtracting using subtract(Number, String)
durationOne.subtract(24, 'months');
console.log(
"Data of durationOne after subtracting:",
durationOne._data
)
console.log(
"Humanized Data of durationOne after subtracting:",
durationOne.humanize()
)
let durationTwo = moment.duration(130, 'seconds');
console.log(
"Data of durationTwo before subtracting:",
durationTwo._data
)
console.log(
"Humanized Data of durationTwo before subtracting:",
durationTwo.humanize()
)
// Subtracting using subtract(Number)
durationTwo.subtract(50000);
console.log(
"Data of durationTwo after subtracting:",
durationTwo._data
)
console.log(
"Humanized Data of durationTwo after subtracting:",
durationTwo.humanize()
)
运行应用程序的步骤: 在终端中输入以下命令来运行index.js文件:
node index.js
输出:
Data of durationOne before subtracting: {
milliseconds: 0,
seconds: 0,
minutes: 0,
hours: 0,
days: 0,
months: 0,
years: 4
}
Humanized Data of durationOne before subtracting: 4 years
Data of durationOne after subtracting: {
milliseconds: 0,
seconds: 0,
minutes: 0,
hours: 0,
days: 0,
months: 0,
years: 2
}
Humanized Data of durationOne after subtracting: 2 years
Data of durationTwo before subtracting: {
milliseconds: 0,
seconds: 10,
minutes: 2,
hours: 0,
days: 0,
months: 0,
years: 0
}
Humanized Data of durationTwo before subtracting: 2 minutes
Data of durationTwo after subtracting: {
milliseconds: 0,
seconds: 20,
minutes: 1,
hours: 0,
days: 0,
months: 0,
years: 0
}
Humanized Data of durationTwo after subtracting: a minute
示例2:
const moment = require('moment');
let durationThree = moment.duration(15, 'hours');
console.log(
"Data of durationThree before subtracting:",
durationThree._data
)
console.log(
"Humanized Data of durationThree before subtracting:",
durationThree.humanize()
)
// Subtracting using subtract(Duration)
let durationTosubtract = moment.duration(72, 'minutes');
durationThree.subtract(durationTosubtract);
console.log(
"Data of durationThree after subtracting:",
durationThree._data
)
console.log(
"Humanized Data of durationThree after subtracting:",
durationThree.humanize()
)
let durationFour = moment.duration(4, 'hours');
console.log(
"Data of durationFour before subtracting:",
durationFour._data
)
console.log(
"Humanized Data of durationFour before subtracting:",
durationFour.humanize()
)
// Subtracting using subtract(Object)
let durationTosubtractAgain = moment.duration(
{ minutes: 120, seconds: 30 }
);
durationFour.subtract(durationTosubtractAgain);
console.log(
"Data of durationFour after subtracting:",
durationFour._data
)
console.log(
"Humanized Data of durationFour after subtracting:",
durationFour.humanize()
)
输出:
Data of durationThree before subtracting: {
milliseconds: 0,
seconds: 0,
minutes: 0,
hours: 15,
days: 0,
months: 0,
years: 0
}
Humanized Data of durationThree before subtracting: 15 hours
Data of durationThree after subtracting: {
milliseconds: 0,
seconds: 0,
minutes: 48,
hours: 13,
days: 0,
months: 0,
years: 0
}
Humanized Data of durationThree after subtracting: 14 hours
Data of durationFour before subtracting: {
milliseconds: 0,
seconds: 0,
minutes: 0,
hours: 4,
days: 0,
months: 0,
years: 0
}
Humanized Data of durationFour before subtracting: 4 hours
Data of durationFour after subtracting: {
milliseconds: 0,
seconds: 30,
minutes: 59,
hours: 1,
days: 0,
months: 0,
years: 0
}
Humanized Data of durationFour after subtracting: 2 hours
参考: https://momentjs.com/docs/#/durations/subtract/