skip to Main Content

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

Answers


  1. 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.

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search