Skip to content

πŸŽ‰ A utility for guessing the format of a datetime ⏰ πŸ™Œ

License

Notifications You must be signed in to change notification settings

third-street/datetime-guess

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Welcome to dotnet-datetime-guess πŸ‘‹

GitHub Workflow Status Release Version Nuget Version Nuget Downloads

πŸŽ‰ A utility package for guessing date's format ⏰ πŸ™Œ

πŸ‘¨β€πŸ’» Usage

Package

Install from Powershell

Nuget-Install 'DateTime-Guess'

Install from .NET CLI

dotnet add package 'DateTime-Guess'

Formats

Format is a public enumerator.

// output default format (Java)
List<string> = Guesser.GuessFormat("Fri, January 30th 2020, 10:00 AM")
List<string> = Guesser.GuessFormat("Fri, January 30th 2020, 10:00 AM", Format.Java)

// output Moment.js format
List<string> = Guesser.GuessFormat("31st Dec, 2020", Format.Moment)

// output strftime format
List<string> = Guesser.GuessFormat("31st Dec, 2020", Format.Linux)

Code Example

using DateTimeGuess;

public class GetDateFormatExample
{
    public static List<string> GetDateFormat(string date)
    {
        try {
            return Guesser.GuessFormat(date, Format.Java);
        }
        catch (Exception e)
        {
            // "Couldn't parse date."
            // "Couldn't find a modifier for x."
        }
    }
}

πŸ™Œ 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 return all possible matched formats.

πŸ” 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).