Skip to content

Should PlainYearMonth throw when adding days? #1731

@devongovett

Description

@devongovett

I find this confusing:

Temporal.PlainYearMonth.from('2019-06').add({days: 500}).toString()
// => '2020-10'

It seems to leak implementation details: PlainYearMonth is the same as PlainDate but with the day field set to 1 by default. Conceptually, PlainYearMonth is meant to represent the entire month, i.e any day within the month, but adding days assumes that it actually represents the first of the month.

This is true of other fields and data types as well, e.g. you can add hours to a PlainDate, which assumes it is midnight by default.

Temporal.PlainDate.from('2019-06-02').add({hours: 24}).toString()
// => '2019-06-03'

I think it would make more sense to be more strict about what fields can be added to each type such that only the smallest field represented by the type is allowed to be added.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions