Skip to content

dalenewman/DateMath

Repository files navigation

Date Math

Build status NuGet

This is a .NET library for parsing "date math" as seen in Elasticsearch. To find out more about date math, read this and this.

You can use this library for setting default report parameters, e.g:

Range Description Start End
last 10 days now-10d now
month to date now/M now
year to date now/y now
a sliding 60 day window now-30d now+30d

Basic Usage

Let now be 2016-12-31 1 PM:

DateMath.Parse("now","yyyy-MM-dd h tt"); // 2016-12-31 1 PM
DateMath.Parse("now+11h", "yyyy-MM-dd h tt"); // 2017-01-01 12 AM
DateMath.Parse("now-13h+1y+1d-1s", "yyyy-MM-dd HH:mm:ss"); // 2017-12-31 23:59:59
DateMath.Parse("now/d", "yyyy-MM-dd HH:mm:ss.fff"); // 2016-12-31 00:00:00.000

If you want to know if your string was parsed successfully:

DateTime date;
if(DateMath.TryParse("now+11h", out date)){
    // do something with date
}

If you already have a date:

DateTime date = new DateTime(2016,12,31,9,30,2);
var newYears = DateMath.Apply(date, "+1d/y");

See tests for other usage.

Releases

No releases published

Packages

No packages published