wanasit / chrono

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

Unexpected output when parsing "MM-DD" format input #571

Closed lemorage closed 3 weeks ago

lemorage commented 1 month ago

I ran the following example test, but got an unexpected output. I thought it should return me 2024-05-22 as the start date, and 2024-05-28 as the end date.

# This test ran on Tue  8 Oct 2024 11:28:23 CST

Input: I will stay there from 5-22 to 5-28.
start:  2024-10-07T21:00:00.000Z
end:  2024-10-08T14:00:00.000Z

After some time of debugging, I thought this should be due to EnTimeExpressionParser being invoked at first. However, I still had no idea why I got this weird output 2024-10-07T21:00:00.000Z ~ 2024-10-08T14:00:00.000Z.

wanasit commented 1 month ago

I'm trying to fix that, but this is tricky (e.g. parsing "from 5-22" as from 5am to 10pm on the same day is also valid).

Is the hyphenated date (e.g. "5-22") common in your locale? I believe if it's more common "5/22 to 5/28", it should work correctly.

lemorage commented 1 month ago

It really took me a while to realize that, 5-22 can be viewed as a time range in a day. At least for my case, I would say it is quite non-intuitive to parse as a time rather than a date. Do u think is there any way I could bypass the time parsing here?

wanasit commented 3 weeks ago

Similar to #570 case, I would suggest removing EnTimeExpressionParser if you know you do not need time (e.g. you are working with date only).

If not, another way is adding a custom refiner to filter out the those specific case:

// Make a custom instance from a normal Chrono (English casual).
const custom = chrono.casual.clone();

// Use `unshift` to filter the results out early before considering combining them with others.
custom.refiners.unshift({
    refine: (context, results) => {
        return results.filter(
            // Filter out '1-22', '12-12' etc
            (r) => !r.text.match(/\d+-\d+/) && !r.start.isTimeOnly() 
        );
    }
});
lemorage commented 3 weeks ago

Cool, I'll try it!