Search code examples
c#datetimeumbracolucene.netexamine

Umbraco Lucene Date Field Encoding convert to .net DateTime


I am trying to update an Umbraco site using Lucene for a site search function.

I am trying to display a list of search results and include the last time the search result was updated.

I found the field on the Examine.Lucene.Search.LuceneSearchResult called "updateDate".

When I check the field I get something like: 638502404046200000 for the value.

Does anyone know how to convert this field to a .net DateTime object?

Currently we used:

public static string LuceneDateTimeStamp(string adt)
{
   DateTime dt;
   adt = adt.Replace("-", "");
   if (adt.Length > 8) adt = adt.Substring(0, 8);

   DateTime.TryParseExact(adt, "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt);
   return dt.ToString("D");
}

However this is not working and gives 1st January 0001. How do you convert the value into something meaningful to display on a search results page?


Solution

  • 638502404046200000 is probably a DateTime.Ticks value:

    public static DateTime ParseLuceneTimeStamp(string value)
    {
        var ticks = long.Parse(value);
        return new DateTime(ticks);
    }