Skip to content

PlainDate.add create a new overflow and force the day after #32

@Joxit

Description

@Joxit

Add a third overflow type on PlainDate.add, PlainDate.subtract... that will return the "day after" when the date does not exists.

Start Date Duration Actual Result Wanted Result
2024-01-01 P1M 2024-02-01 ✔️ 2024-02-01
2024-01-15 P1M 2024-01-15 ✔️ 2024-01-15
2024-01-31 P1M 2024-02-28 ⚠️ 2024-03-01
2024-01-31 P1M1D 2024-03-01 ✔️ 2024-03-01
2024-02-29 P2Y 2026-02-28 ⚠️ 2026-03-01

Advantages:

The actual way to achieve the goal is using a try-catch which is not really intuitive.

try {
  Temporal.PlainDate.from('2024-01-31')
    .add('P1M', { overflow: 'reject' })
    .toString()
  // throw RangeError
  // with { overflow: 'constrain' } the result is '2024-02-29'
} catch(e) {
  Temporal.PlainDate.from('2024-01-31')
    .add('P1M1D', { overflow: 'constrain' })
    .toString()
  // '2024-03-01'
}

This can be useful for statistics, when you want to group them by month with different anchor dates and the day generated by the + P1M should not be included in the range.
Meaning 1 month starting from 2024-01-31 must include 2024-02-28 and one month starting from 2024-01-01 must not include 2024-02-01.

Concerns:

None

Prior art:

I check the behaviour on Java/Kotlin (with LocalDate) and rust (with chrono). They both have the same behavior as { overflow: 'constrain' }. Even the reject behavior does not exists when adding a month.

Constraints / corner cases:

For an anchor day at the end of a month e.g. 2024-01-31, the result table I want should be something like:

Start Date Duration End Date
2024-01-31 +P1M 2024-03-01
2024-01-31 +P2M 2024-03-31
2024-01-31 +P3M 2024-05-01
2024-01-31 +P4M 2024-05-31

see tc39/proposal-temporal#2936

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