datetime_add

This page explains how to use the datetime_add function in APL.

Use the datetime_add function in APL to calculate a new datetime by adding a specified number of date parts to a base datetime value. You can add years, months, weeks, days, hours, minutes, seconds, or smaller units. Use negative values to subtract.

You can use datetime_add to shift timestamps forward or backward for time-window comparisons, expiration calculations, and timezone adjustments.

Use it when you want to:

  • Project future or past timestamps relative to an event.
  • Define time ranges around a known incident or deadline.
  • Shift trace or log timestamps for timezone normalization.

Usage

Syntax

datetime_add(part, value, datetime)

Parameters

NameTypeDescription
partstringThe unit of time to add: 'year', 'quarter', 'month', 'week', 'day', 'hour', 'minute', 'second', 'millisecond', 'microsecond'.
valueintThe number of units to add. Use a negative value to subtract.
datetimedatetimeThe base datetime value.

Returns

A datetime value after adding the specified interval to the base datetime.

Use case examples

Project what the time is 1 hour after each request to estimate cache expiration windows.

Query

['sample-http-logs']
| extend future_time = datetime_add('hour', 1, _time)
| project _time, future_time, method, status

Run in Playground

Output

_timefuture_timemethodstatus
2025-01-15T10:00:00Z2025-01-15T11:00:00ZGET200
2025-01-15T10:05:00Z2025-01-15T11:05:00ZPOST201
2025-01-15T10:12:00Z2025-01-15T11:12:00ZGET404

This query adds 1 hour to each request timestamp, which is useful for estimating when cached responses expire.

Shift trace timestamps forward by 30 minutes to simulate a timezone adjustment.

Query

['otel-demo-traces']
| extend adjusted_time = datetime_add('minute', 30, _time)
| project _time, adjusted_time, ['service.name'], duration

Run in Playground

Output

_timeadjusted_time['service.name']duration
2025-01-15T08:00:00Z2025-01-15T08:30:00Zfrontend00:00:01.2340000
2025-01-15T08:01:00Z2025-01-15T08:31:00Zcart00:00:00.5670000
2025-01-15T08:02:00Z2025-01-15T08:32:00Zcheckout00:00:02.1000000

This query shifts each trace timestamp forward by 30 minutes, useful for aligning traces from systems that report in different time offsets.

Find requests that occurred within 1 day before a known incident time.

Query

['sample-http-logs']
| where _time between (datetime_add('day', -1, datetime(2025-01-15)) .. datetime(2025-01-15))
| summarize count() by status, method

Run in Playground

Output

statusmethodcount_
200GET1204
500POST37
404GET89

This query uses datetime_add to define a 1-day window before a known incident, helping you investigate the activity that preceded it.

  • datetime_diff: Calculates the difference between two datetime values. Use when you need to measure elapsed time rather than shift a timestamp.
  • ago: Subtracts a timespan from the current UTC time. Use for simple relative time filters based on now().
  • now: Returns the current UTC time.
  • startofmonth: Returns the start of the month for a datetime, useful for month-boundary calculations.
  • endofmonth: Returns the end of the month for a datetime.

Other query languages