@gristlabs/moment-guess

中文文档
1.2.4-grist.1发布于 2022/02/21MITESM + CJS近 30 天下载 9732

A utility package for guessing date's format

原始使用文档
文档目录12 节
  1. 👨‍💻 Usage
  2. 💻 CLI
  3. 📦 Package
  4. 🙌 Supported Date Formats
  5. 🤷‍♀️ What happens in case of ambiguous input?
  6. :thinking: Motivation
  7. :hammer: Run tests
  8. :mag: How does it work?
  9. :man: Author
  10. 🤝 Contributing
  11. Show your support
  12. 📝 License

Welcome to moment-guess 👋

Version Documentation Maintenance License: MIT

:tada: A utility package for guessing date's format :alarm_clock: :raised_hands:

Demo

👨‍💻 Usage

💻 CLI

# output default format
npx moment-guess --date "Fri, January 30th 2020, 10:00 AM"

# output strftime format
npx moment-guess --date "31st Dec, 2020" --format strftime

For details, try npx moment-guess --help

📝 Note: The above commands DO NOT INSTALL the package system-wide, npx is just a package runner.

📦 Package

npm install moment-guess
const guessFormat = require('moment-guess');

// default format
console.log(guessFormat("31/12/2020")); // DD/MM/YYYY

// default format
console.log(guessFormat("01/01/2020 10:00 AM PST", "default")); // [ 'DD/MM/YYYY hh:mm A z', 'MM/DD/YYYY hh:mm A z' ]

// strftime format
console.log(guessFormat("Fri, January 30th 2020, 10:00 AM", "strftime")); // %a, %B %o %Y, %I:%M %p

// Errors!
try {
    console.log(guessFormat("Invalid date!"));
} catch (err) {
    console.log(err.message); // Couldn't parse date
}

try {
    console.log(guessFormat("Mo, 23rd Nov, 2020", "strftime"));
} catch(err) {
    console.log(err.message); // Couldn't find strftime modifier for "Mo"
}

🙌 Supported Date Formats

  • 2020-07-24T17:09:03+00:00(IS0 8601)

  • 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)

  • 31-Dec-2020, 1-Jan-20(dash delimited with month name)

  • 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"

:thinking: Motivation

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?

:hammer: Run tests

npm run test

:mag: 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).

:man: Author

Apoorv Mishra

🤝 Contributing

Contributions, issues and feature requests are welcome!
Feel free to check issues page.

Show your support

Give a ⭐️ if this project helped you!

📝 License

Copyright © 2020 Apoorv Mishra.
This project is MIT licensed.


This README was generated with ❤️ by readme-md-generator

版本与发布记录

@gristlabs/moment-guess 共发布 1 个版本,首次发布于 2022/02/21;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.2.4-grist.1latest2022/02/214 年前

关于 @gristlabs/moment-guess 的常见问题

@gristlabs/moment-guess 是什么?

A utility package for guessing date's format

如何安装 @gristlabs/moment-guess?

在项目目录运行 npm install @gristlabs/moment-guess,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 moment-guess,可以用 npx moment-guess 直接运行。

@gristlabs/moment-guess 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/gristlabs__moment-guess 是否存在。

@gristlabs/moment-guess 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@gristlabs/moment-guess 可以商用吗?

@gristlabs/moment-guess 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@gristlabs/moment-guess 的下载量有多少?

根据 npm 官方下载统计,@gristlabs/moment-guess 在 2026/10/17 – 2026/11/15 这 30 天内被下载 9,732 次,日均约 324 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@gristlabs/moment-guess 还在维护吗?

最新版本 1.2.4-grist.1 发布于 2022/02/21(4 年前);至今共发布 1 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/02/21。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。