wanasit / chrono

A natural language date parser in Javascript

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`forwardDate: true` and day of the week happens to be today?

bytrangle opened this issue · comments

It's a bit hard to explain in the title, but what happens if:

  • forwardDate is set to true
  • today is Wednesday
  • the input date contains "Wednesday"

Then the result will be the next Wednesday, 7 days from now, it seems. Is there any way to interpret it as Wednesday of today? The way forwardDate: true works in other scenarios is good for me.

Hello @bytrangle. Sorry for my slow reply.
Could you explain more about your use case?

Without the time input mentioned, "Wednesday" is parsed as "Wednesday at 12pm" (midday). So:

  • if your reference time (now) is before 12, the "Wednesday" is today.
  • if your reference time (now) is after 12, because forwardDate, the "Wednesday" is next week.
chrono.parseDate("Wednesday", new Date("Wed Dec 21 2022 7:50:22"), { forwardDate: true }) 
// should be "Wed Dec 21..."

chrono.parseDate("Wednesday", new Date("Wed Dec 21 2022 13:50:22"), { forwardDate: true }) 
// should be "Wed Dec 28..."

If the time doesn't matter, I suggest using the beginning of the day as your reference instead of now.

At 9pm on a Wednesday I wrote out remind me wednesday at 10pm to test and it resolves to 10pm on the same day even forwardDate true. The code is like this: chrono.parse(reminderInput, new Date(), { forwardDate: true })

@wanasit Is this expected? It seems incorrect and it seems not in line with your explanation above, but maybe I'm misinterpreting?

Yes. It'd work like that currently. It would not say it's "correct", but the { forwardDate: true } simply means the output must be later than the reference date.

In your example, "Wednesday at 10pm" is already later than the reference (Wednesday at 9pm), thus it does not assume it would need to add another week.

I'll see if this could be improve by applying forwardDate on date and time separately.
For now, I'm afraid you would have to check for that manually.

@wanasit have any ideas for fixing this occurred to you? Not looking for an implementation of course, but I wonder if your subconscious has floated you any good ideas for resolving this edge case?

I have not time to change and test it, but I am thinking rather than rely on ForwardDateRefiner to make the change, createParsingComponentsAtWeekday() in weekday.ts should detect the forward date options.