chrono icon indicating copy to clipboard operation
chrono copied to clipboard

Is it possible to support MMDDYYYY without hyphens or slashes?

Open daniel-gener8tor opened this issue 3 years ago • 3 comments

daniel-gener8tor avatar Oct 25 '22 05:10 daniel-gener8tor

Sorry for my slow reply. I don't know if we want to have that in the default Chrono.

Would adding a custom parser work for your case? https://github.com/wanasit/chrono#parser

const custom = chrono.casual.clone();
custom.parsers.push({
    pattern: () => { return /\b(\d{2})(\d{2})(\d{4})\b/i },
    extract: (context, match) => {
        return {
            day: parseInt(match[2]), 
            month: parseInt(match[1]), 
            year: parseInt(match[3]), 
        }
    }
});

custom.parseDate("I'll arrive at 01142023");

wanasit avatar Jan 14 '23 08:01 wanasit

Hi @wanasit

I know this is a very old issue but I wrote the exact same custom parser as your code above and it does not function as expected. The parsed result returned is []

I can confirm with debug statements that the pattern matches and the extract function is executed. However it does not return any results.

I tested your code above and my own version of it and neither work as expected. Do you have insight into this issue ?

arunr14 avatar Apr 25 '24 00:04 arunr14

We have identified the "UnlikelyFormatFilter" refiner as the reason why the parsedResult is being filtered out. We worked around it by replacing the refiner with a version that does NOT do the following refinement

if (result.text.replace(" ", "").match(/^\d*(.\d*)?$/)) { context.debug(() => { console.log(Removing unlikely result '${result.text}'); });

        return false;
    }

At the moment we are replacing the refiner using its index. We tried using constructor name but that did not work on production.

@wanasit could you expose the refiner names so users are able to replace/modify them as required.

arunr14 avatar Apr 26 '24 20:04 arunr14