wanasit / chrono

A natural language date parser in Javascript
MIT License
4.61k stars 339 forks source link

`forwardDate: false` moves date forward after 6 months #469

Open peterjanes opened 2 years ago

peterjanes commented 2 years ago

I'd expect that parsing a day with no year would imply using the year of the reference date, but if the reference is more than 6 months after the relative date then the year is advanced.

> // right
> chrono.parseDate('March 21', new Date('2022-09-20'), { forwardDate: false });
2022-03-21T16:00:00.000Z
> // wrong
> chrono.parseDate('March 21', new Date('2022-09-21'), { forwardDate: false });
2023-03-21T16:00:00.000Z
> // right
> chrono.parseDate('March 21', new Date('2021-09-20'), { forwardDate: false });
2021-03-21T16:00:00.000Z
> // wrong
> chrono.parseDate('March 21', new Date('2021-09-21'), { forwardDate: false });
2022-03-21T16:00:00.000Z
Nantris commented 2 years ago

You need to use forwardDate: true

peterjanes commented 2 years ago

@Slapbox That will force the date to move into the next year, which is exactly what I don't want.

forwardDate (boolean) to assume the results should happen after the reference date (forward into the future)

Nantris commented 2 years ago

Ah I see I misunderstood the issue - apologies.

sobjornstad commented 5 months ago

We're having a related but opposite undesired behavior: dates that are more than 6 months in the future are rolled back into the previous year. Like @peterjanes, I would expect the year to be set to the current year if unspecified.

chrono.parseDate('December 31', new Date('2024-06-01'))
Sun Dec 31 2023 12:00:00 GMT-0600 (Central Standard Time)

Using forwardDate doesn't work for this either, because then relative dates like “last Friday” get interpreted as future dates (which seems pretty wonky; I would expect the parser to give up rather than interpret “last Friday” as a date in the future).