Relative Date-Time
  • 14 Oct 2021
  • 4 Minutes to read
  • Dark
    Light

Relative Date-Time

  • Dark
    Light

Article summary

Note:
This document builds on the date-time principles introduced in the Query Syntax guide.

The query term for a date-time interval now supports date delta expressions whenever the begin date-time or the end date-time uses the keyword TODAY or NOW. It is awkward for the user to write a different fixed date-time interval each time he wants to query over the last 30 days, for example:

service_date:[2015-10-06  TO  2015-11-05]

He may now write a relative date-time expression to cover a date-time interval relative to the current date (or date-time) eliminating the need to change the dates in the query each time he runs the report. To do so, a pivot term (fixed term) is required as well as a delta term (the increment from the pivot term).


Delta Operators

The delta refers to the date or time as it revolves around the pivot term. It is possible to use either a positive or a negative term in the search.

Below is a list of operators used to increment or decrement the date or time:

  • ±1s: second
  • ±1mi: minute
  • ±1h: hour
  • ±1d: day
  • ±1w: week
  • ±1mo: month
  • ±1q: quarter
  • ±1y: year

To increment or decrement more than one unit, enter the appropriate number instead of 1. For exactly one unit, the 1 is optional. The following queries over the last 30 days:

service_date:[TODAY-30d  TO  TODAY]
Note:
When using the plus sign, +, in a browser's URL line, it expects HTTP CGI encoding. This means that the + symbol must be percent encoded as %2B; otherwise, it gets converted to a blank. However, when entering data into the code section of Report Builder, the software typically does the percent encoding, and the + should be entered as +.

Pivot Term

The keyword TODAY is used to refer to the current date.

The keyword NOW is used to refer to the current date-time (accurate to the nearest second).

As a convenience, the keyword TODAY (or NOW) only needs to appear in one of the terms, but one of the keywords must appear in at least one term. The other term may be a pure date delta expression:

service_date:[-30d  TO  TODAY]

As long as one term resolves to an absolute date-time, known as the "pivot," the other term is computed from the pivot value.

Here the ending date is the day prior to when the query was executed, and the start date is 30 days before that. The pivot is TODAY minus one day (yesterday). The start date is the pivot value minus 30 days.

service_date:[-30d  TO  TODAY-1d]

The pivot can be in the start term:

appointment_date:[TODAY+d TO 30d]

Note in this example the "1" was optional: "+d" is the same as "+1d." Also, the lead "+" was inferred for "30d" (same as "+30d"). The example indicates tomorrow is the pivot (TODAY plus one day), and the end date is tomorrow plus thirty days.

Note:
Use caution when using TODAY or NOW as the start term and using a future date or time for the ending term. Historic cases require a backward search rather than forward.

Date Delta with Absolute Date-time Strings

An absolute date string may be entered instead of the word TODAY or NOW. Replacing an absolute date or time performs the same functionality as a pivot term.

service_date:[-1mo TO 2015-11-06]
service_date:[-1mo TO 20151106]

Note: The minus operator, -, can clash with the hyphen in a date string. This can happen if the position of the minus is just after the year: service_date:[-1y TO 2015-10y]. The parser treats the hyphen as part of the date string and issues an error message.

To correct the ambiguity, enter a space after the year: service_date:[-1y TO 2015 -10y].


Round Down, <, and Round Up, >, Operators

The < and > symbols are used to round the relative date-time down or up to the boundary indicated by the time unit (minute, hour, day, week, month, quarter or year).

The following example rounds TODAY down to the beginning of the year, causing 2015-01-01 (given 2015 is the current year) to be the pivot. The start date is one year earlier: 2014-01-01. Rounding always results in a legitimate date.

service_date:[-1year TO TODAY<year]
service_date:[-1y TO TODAY<y]

The below examples search for the service dates of last week. "TODAY<w" rounds down to the beginning of this week (to become the pivot). The start date is one week prior to that date.

service_date:[-1week TO TODAY<week]
service_date:[-w TO TODAY<w]

Because of the inclusive brackets, [ ], both start-of-week days are included in the search, thus spanning eight days. Use an exclusive brace, { or }, to eliminate the start day or the end day:

service_date:[-w TO TODAY<w}
service_date:{-w TO TODAY<w]
service_date:{-w TO 20151106<w]

By convention, the start of the week is on a Sunday. The first example spans seven days starting on a Sunday. The next two examples span seven days starting on a Monday. Alternatively, a compound date delta expression can be used to alter the beginning and ending dates.


Compound Date Delta Expressions

Multiple date delta terms can be combined to demark the appropriate date as long as they have explicit operators separating them:

service_date:[-w+d  TO  TODAY<w]

The above's start date has one day added to the beginning of the previous week making the start date begin on a Monday.

This is a more complex example:

appointment_date:[-y+2mo+5d  TO  TODAY>mo+2w]

The pivot date is the end of this month plus two weeks. The start date is last year plus two months plus five days computed from the pivot date.


Was this article helpful?