3

Using LUIS engine, I receive XXXX-10-28 as a date in the Entity Value.

I've tried using Chronic to parse but Chronic does not work with timex library/formats.

I'm expecting following strings as inputs

  • XXXX-10-28 should equate to 2018-10-28 (future)
  • 2018-10-02TMO should equate to 2018-10-02 i.e. tomorrow

Please note that XXXX-XX represents YYYY-MM but it doesn't have numeric values

Is there any library, or way to parse such strings to a valid datetime format in ASP.NET Core?

GeekzSG
  • 943
  • 1
  • 11
  • 28
  • Timex is a libray, not a format. What you posted looks like `YYYY-MM-DD`, the ISO8601 date format which can be parsed out-of-the-box by `DateTime.Parse`. For example `DateTime.Parse("2018-11-13")` will return a DateTime object whose value is November 11, 2018 – Panagiotis Kanavos Oct 01 '18 at 15:04
  • @PanagiotisKanavos Timex is actually a format. It's used by the [Datetimev2 prebuilt entity for LUIS](https://learn.microsoft.com/en-us/azure/cognitive-services/luis/luis-reference-prebuilt-datetimev2). [Find the spec here.](http://www.timeml.org/tempeval2/tempeval2-trial/guidelines/timex3guidelines-072009.pdf) – Wessel T. Jun 13 '19 at 21:21
  • Note that `2018-10-02TMO` does not mean tomorrow, but 'in the morning of 2018-10-02'. – Wessel T. Jun 13 '19 at 21:46

3 Answers3

2

You can use the Microsoft.Recognizers.Text.DataTypes.TimexExpression package from NuGet. It's part of the Microsoft Recognizers Text project here on github

I found two ways you can use this library:

Parse the expression using a TimexProperty and guess the year yourself:

var parsed = new Microsoft.Recognizers.Text.DataTypes.TimexExpression.TimexProperty("XXXX-10-28");
Console.WriteLine(parsed.Year); // = null
Console.WriteLine(parsed.Month); // = 28
Console.WriteLine(parsed.DayOfMonth); // = 10

Resolve times useing the TimexResolver

var resolution = Microsoft.Recognizers.Text.DataTypes.TimexExpression.TimexResolver.Resolve(new [] { "XXXX-10-28" }, System.DateTime.Today)

The resolution.Values will contain an array with two resolution entries, one for the previous occurrence of that date and one for the next occurrence of that date (based on the DateTime you pass into the Resolve method.

Note that from personal experience and from what I saw on github, that at the time of writing, this package can be quite buggy with the more advanced expressions.

Wessel T.
  • 2,280
  • 2
  • 20
  • 29
1

Use a custom format pattern:

using System;
using System.Globalization;
class MainClass {
  public static void Main (string[] args) {
    var format = "1234-10-30";
    var date = DateTime.ParseExact(format, "yyyy-MM-dd", CultureInfo.InvariantCulture);
    Console.WriteLine (date.ToString("dd/MM/yyyy"));
  }
}

Example

BanksySan
  • 27,362
  • 33
  • 117
  • 216
0

You can use DateTime.TryParse or DateTime.TryParseExact along with a custom date/time format string in order to accomplish this.

Based on your example, I think the format string you'd want is yyyy-MM-dd, although you may need to tweak this slightly.

Example:

var input = "2018-10-28";
var format = "yyyy-MM-dd";
DateTime parsed;
if (DateTime.TryParseExact(input, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out parsed))
{
    // Do whatever you want with "parsed"
}
Donut
  • 110,061
  • 20
  • 134
  • 146