The library supports full Elasticsearch date math syntax for defining ranges:
- Anchors:
now, or explicit dates using the || separator. - Operations:
+1d (add), -1h (subtract), /d (round). - Units:
y (years), M (months), w (weeks), d (days), h/H (hours), m (minutes), s (seconds). - Timezone Support: Preserves explicit timezones (
Z, +05:00, -08:00) or falls back to the system timezone. - Escaped slashes:
\/d is treated identically to /d.
// Common date range queries
var last15Minutes = DateTimeRange.Parse("[now-15m TO now]", now); // 15 minutes ago through now
var lastHour = DateTimeRange.Parse("[now-1h/h TO now-1h/h]", now); // Start through end of last hour
var last24Hours = DateTimeRange.Parse("[now-24h TO now]", now); // 24 hours ago through now
var today = DateTimeRange.Parse("[now/d TO now/d]", now); // Start of day through end of day
var yesterday = DateTimeRange.Parse("[now-1d/d TO now-1d/d]", now); // Start through end of yesterday
var tomorrow = DateTimeRange.Parse("[now+1d/d TO now+1d/d]", now); // Start through end of tomorrow
var last7Days = DateTimeRange.Parse("[now-7d/d TO now-1d/d]", now); // Last 7 full days (not including today)
var thisWeek = DateTimeRange.Parse("[now/w TO now/w]", now); // Start of week through end of week
var lastWeek = DateTimeRange.Parse("[now-1w/w TO now-1w/w]", now); // Start through end of last week
var thisMonth = DateTimeRange.Parse("[now/M TO now/M]", now); // Start of month through end of month
var lastMonth = DateTimeRange.Parse("[now-1M/M TO now-1M/M]", now); // Start through end of last month
var yearToDate = DateTimeRange.Parse("[now/y TO now]", now); // Start of year through now
var lastYear = DateTimeRange.Parse("[now-1y/y TO now-1y/y]", now); // Start through end of last year
// Short-form comparison operators
var recentItems = DateTimeRange.Parse(">=now-1h", now); // From 1 hour ago to max
var futureOnly = DateTimeRange.Parse(">now", now); // After now to max
var beforeToday = DateTimeRange.Parse("<now/d", now); // Min to start of today
var throughToday = DateTimeRange.Parse("<=now/d", now); // Min to end of today