datediff()

Returns the difference between the start date and end date in the specified time unit.

Syntax

datediff(START_DATE, END_DATE, "{year|mon|day|hour|min|sec|msec}")
Required Parameter
START_DATE
Expression that returns the start date. The function returns null if a value of any other type is received.
END_DATE
Expression that returns the last date. The function returns null if a value of any other type is received.
"{year|mon|day|hour|min|sec|msec}"
Time unit to use when calculating the difference between START_DATE and END_DATE, enclosed in a pair of double quotes. For the meaning of each unit of time, refer to the table below.

Unit of Time

Unit of TimeDescription
yearYear
monMonth
dayDay
hourHour
minMinute
secSecond
msecMillisecond

Usage

  1. Calculate the difference between September 29, 2014, and September 29, 2013.

    json "{}"
    | set start=date("2013-09-29", "yyyy-MM-dd")
    | set end=date("2014-09-29", "yyyy-MM-dd")
    | eval year = datediff($("start"), $("end"), "year"),
           mon  = datediff($("start"), $("end"), "mon"),
           day  = datediff($("start"), $("end"), "day"),
           hour = datediff($("start"), $("end"), "hour"),
           min  = datediff($("start"), $("end"), "min"),
           sec  = datediff($("start"), $("end"), "sec"),
           msec = datediff($("start"), $("end"), "msec")
    
  2. In the case of an incorrect input

    json "{}"
    | eval
        error0 = datediff(null, date("2014-09-29", "yyyy-MM-dd"), "sec"),
        error1 = datediff(date("2013-09-29", "yyyy-MM-dd"), null, "min"),
        error2 = datediff("invalid", date("2014-09-29", "yyyy-MM-dd"), "min")