https://github.com/third-street/datetime-guess
🎉 A utility for guessing the format of a datetime ⏰ 🙌
https://github.com/third-street/datetime-guess
datetime dotnet guesser timestamp
Last synced: 8 days ago
JSON representation
🎉 A utility for guessing the format of a datetime ⏰ 🙌
- Host: GitHub
- URL: https://github.com/third-street/datetime-guess
- Owner: third-street
- License: mit
- Created: 2021-07-26T21:13:29.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2023-08-29T14:09:46.000Z (over 2 years ago)
- Last Synced: 2024-08-30T13:50:02.119Z (over 1 year ago)
- Topics: datetime, dotnet, guesser, timestamp
- Language: C#
- Homepage: https://www.nuget.org/packages/DateTimeGuess/
- Size: 829 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Welcome to dotnet-datetime-guess 👋




> :tada: A utility package for guessing date's format :alarm_clock: :raised_hands:
## 👨💻 Usage
### Package
Install from Powershell
```ps1
Nuget-Install 'DateTime-Guess'
```
Install from .NET CLI
```ps1
dotnet add package 'DateTime-Guess'
```
### Formats
Format is a public enumerator.
```c#
// output default format (Java)
List = Guesser.GuessFormat("Fri, January 30th 2020, 10:00 AM")
List = Guesser.GuessFormat("Fri, January 30th 2020, 10:00 AM", Format.Java)
// output Moment.js format
List = Guesser.GuessFormat("31st Dec, 2020", Format.Moment)
// output strftime format
List = Guesser.GuessFormat("31st Dec, 2020", Format.Linux)
```
### Code Example
```c#
using DateTimeGuess;
public class GetDateFormatExample
{
public static List 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](https://en.wikipedia.org/wiki/ISO_8601))
- *Mon, 06 Mar 2017 21:22:23 +0000*([RFC 2822](https://tools.ietf.org/html/rfc2822#section-3.3))
- *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**.
## :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](https://momentjs.com/docs/#/displaying/) to each corresponding token based on the meaning given to the token by the parser(example, *YYYY* for a four digit year token).