Let's say that I know an unit and an amount of this unit. Is there a way to get a Duration object from it?
Something like Duration.fromUnitAmount(unit: DurationUnit, amount: number): Duration
or DurationUnit.getDuration(amount: number): Duration
, or to build a Duration
object from an object such as { unit: myUnit, amount: myAmount }
.
I've made a function for it, but I wanted to know if something was built-in for this. Here is the function:
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});
}
}
Does this help:
import { Duration, DurationUnit } from 'luxon';
function getDuration(unit: DurationUnit, amount: number) {
return Duration.fromObject({
[unit]: amount,
});
}