🎉A utility package for guessing date's format ⏰ 🙌
📝 Note: The following commands DO NOT INSTALL the package system-wide, permanently. npx is just a package runner.
npx moment-guess --date 'Fri, January 30th 2020, 10:00 AM'
For details, try
npx moment-guess --help
🙌 Supported Formats
Mon, 06 Mar 2017 21:22:23 +0000(RFC 2822)
31/12/2020, 1.1.2020, 31-12-20(slash, dot or dash delimited dates, both US and UK styles)
Fri, January 30th 2020, 10:00 AM(dow, dd Mon yyyy[, hh:mm:ss am|pm|AM|PM] with both short and long names)
🤷♀️ What happens in case of ambiguous input?
If the input is ambiguous like 01/01/2020(could mean DD/MM/YYYY or MM/DD/YYYY), it would display all possible matched formats. Try
npx -q moment-guess -d '01/01/2020'
PM: Hey, could you please display date on the rightmost part of the row in a format similar to Fri, 6th March 2020, 10:00 AM?
Me: Sure. (types on screen ... moment(timestampFromDB).format(// Uh Oh!, what should I put here?))
Methinks: Hmm...such requirements pop up once in a while, not everyday(at least for me). And everytime it happens, I end up sifting through momentjs docs here to choose the right format tokens in order to display date in the desired format("ddd, Do MMMM YYYY, hh:mm A" in this case). Can this be avoided? Is it possible to guess a date's format from the desired date value itself?
🔨 Run tests
npm run test
🔍 How does it work?
Entire module is split up into three main components, parsers, refiners and assigners.
Parsers break the input into individual tokens, giving meaning to each token(whether it's year, month, day...).
Refiners refine the parsed results based on certain chosen heuristics in case the input matched multiple parsers.
Assigners assign the appropriate format tokens(don't confuse these with generated tokens from input) enlisted here to each corresponding token based on the meaning given to the token by the parser(example, YYYY for a four digit year token).
Contributions, issues and feature requests are welcome!
Feel free to check issues page.
Show your support
This README was generated with