Giter Site home page Giter Site logo

0x2539 / moment-guess Goto Github PK

View Code? Open in Web Editor NEW

This project forked from gristlabs/moment-guess

0.0 0.0 0.0 760 KB

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

License: MIT License

JavaScript 50.93% TypeScript 49.07%

moment-guess's Introduction

Welcome to moment-guess ๐Ÿ‘‹

Version Documentation Maintenance License: MIT

๐ŸŽ‰ A utility package for guessing date's format โฐ ๐Ÿ™Œ

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"

๐Ÿค” 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?

๐Ÿ”จ 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).

๐Ÿ‘จ 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

moment-guess's People

Contributors

apoorv-mishra avatar alexmojaki avatar apoorv-ct avatar 0x2539 avatar deepsourcebot avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.