从单位获取持续时间
Get a Duration from the unit
假设我知道一个单位和这个单位的数量。有没有办法从中获取 Duration 对象?
类似于 Duration.fromUnitAmount(unit: DurationUnit, amount: number): Duration
或 DurationUnit.getDuration(amount: number): Duration
,或者从 { unit: myUnit, amount: myAmount }
.
等对象构建 Duration
对象
编辑
我已经为它创建了一个函数,但我想知道是否为此内置了一些东西。这是函数:
static getDurationFromAmountOfUnit(unit: DurationUnit, amount: number) {
switch (unit) {
case 'years':
case 'year':
return Duration.fromObject({year: amount});
case 'quarters':
case 'quarter':
return Duration.fromObject({quarter: amount});
case 'months':
case 'month':
return Duration.fromObject({month: amount});
case 'weeks':
case 'week':
return Duration.fromObject({week: amount});
case 'days':
case 'day':
return Duration.fromObject({day: amount});
case 'hours':
case 'hour':
return Duration.fromObject({hour: amount});
case 'minutes':
case 'minute':
return Duration.fromObject({minute: amount});
case 'seconds':
case 'second':
return Duration.fromObject({second: amount});
case 'milliseconds':
case 'millisecond':
return Duration.fromObject({millisecond: amount});
}
}
这有帮助吗:
import { Duration, DurationUnit } from 'luxon';
function getDuration(unit: DurationUnit, amount: number) {
return Duration.fromObject({
[unit]: amount,
});
}
假设我知道一个单位和这个单位的数量。有没有办法从中获取 Duration 对象?
类似于 Duration.fromUnitAmount(unit: DurationUnit, amount: number): Duration
或 DurationUnit.getDuration(amount: number): Duration
,或者从 { unit: myUnit, amount: myAmount }
.
Duration
对象
编辑
我已经为它创建了一个函数,但我想知道是否为此内置了一些东西。这是函数:
static getDurationFromAmountOfUnit(unit: DurationUnit, amount: number) {
switch (unit) {
case 'years':
case 'year':
return Duration.fromObject({year: amount});
case 'quarters':
case 'quarter':
return Duration.fromObject({quarter: amount});
case 'months':
case 'month':
return Duration.fromObject({month: amount});
case 'weeks':
case 'week':
return Duration.fromObject({week: amount});
case 'days':
case 'day':
return Duration.fromObject({day: amount});
case 'hours':
case 'hour':
return Duration.fromObject({hour: amount});
case 'minutes':
case 'minute':
return Duration.fromObject({minute: amount});
case 'seconds':
case 'second':
return Duration.fromObject({second: amount});
case 'milliseconds':
case 'millisecond':
return Duration.fromObject({millisecond: amount});
}
}
这有帮助吗:
import { Duration, DurationUnit } from 'luxon';
function getDuration(unit: DurationUnit, amount: number) {
return Duration.fromObject({
[unit]: amount,
});
}