Difference between duration units that end with an "s" or not (e.g. "day" vs. "days")

73 Views Asked by At

Using Luxon.js, is there a difference between duration units that end with an "s" and those that do not?

For example:

DateTime.now().plus({ day: 1 });

vs.

DateTime.now().plus({ days: 1 });

My tests seem to indicate that there is no difference, but I want to be sure. I haven't found anything in the documentation about this.

2

There are 2 best solutions below

0
On BEST ANSWER

They are the same.

If you check the source for the Duration object you can see that there's a mapping which converts day to days, minute to minutes and so on for converting all singular time periods to plural.

0
On

Poking through the source, if you pass an object to plus, that eventually gets standardized here to the plural form:

static normalizeUnit(unit) {
    const normalized = {
      year: "years",
      years: "years",
      quarter: "quarters",
      quarters: "quarters",
      . . .
      millisecond: "milliseconds",
      milliseconds: "milliseconds",
    }[unit ? unit.toLowerCase() : unit];

    if (!normalized) throw new InvalidUnitError(unit);

    return normalized;
  }

The object is given to Duration.fromObject, and then given to the above function.