🎉 A utility package for guessing date's format ⏰ 🙌
Install from Powershell
Nuget-Install 'DateTime-Guess'
Install from .NET CLI
dotnet add package 'DateTime-Guess'
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)
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."
}
}
}
-
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)
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.
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).