npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

moment-guess

v1.2.4

Published

A utility package for guessing date's format

Downloads

23,345

Readme

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

👨‍💻 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