Documentation

date.truncate() function

date.truncate() returns a time truncated to the specified duration unit.

Function type signature
(t: A, unit: duration, ?location: {zone: string, offset: duration}) => time where A: Timeable
  • Copy
  • Fill window

For more information, see Function type signatures.

Parameters

t

(Required) Time to operate on.

Use an absolute time, relative duration, or integer. Durations are relative to now().

unit

(Required) Unit of time to truncate to.

Only use 1 and the unit of time to specify the unit. For example: 1s, 1m, 1h.

location

Location used to determine timezone. Default is the location option.

Examples

Truncate time values

import "date"
import "timezone"

option location = timezone.location(name: "Europe/Madrid")

date.truncate(t: 2019-06-03T13:59:01Z, unit: 1s)

// Returns 2019-06-03T13:59:01.000000000Z
date.truncate(t: 2019-06-03T13:59:01Z, unit: 1m)

// Returns 2019-06-03T13:59:00.000000000Z
date.truncate(t: 2019-06-03T13:59:01Z, unit: 1h)

// Returns 2019-06-03T13:00:00.000000000Z
date.truncate(t: 2019-06-03T13:59:01Z, unit: 1d)

// Returns 2019-06-02T22:00:00.000000000Z
date.truncate(t: 2019-06-03T13:59:01Z, unit: 1mo)

// Returns 2019-05-31T22:00:00.000000000Z
date.truncate(t: 2019-06-03T13:59:01Z, unit: 1y)// Returns 2018-12-31T23:00:00.000000000Z
  • Copy
  • Fill window

Truncate time values using relative durations

import "date"

option now = () => 2020-01-01T00:00:30.5Z

date.truncate(t: -30s, unit: 1s)

// Returns 2019-12-31T23:59:30.000000000Z
date.truncate(t: -1m, unit: 1m)

// Returns 2019-12-31T23:59:00.000000000Z
date.truncate(t: -1h, unit: 1h)// Returns 2019-12-31T23:00:00.000000000Z
  • Copy
  • Fill window

Query data from this year

import "date"

from(bucket: "example-bucket")
    |> range(start: date.truncate(t: now(), unit: 1y))
  • Copy
  • Fill window

Query data from this calendar month

import "date"

from(bucket: "example-bucket")
    |> range(start: date.truncate(t: now(), unit: 1mo))
  • Copy
  • Fill window

Was this page helpful?

Thank you for your feedback!


The future of Flux

Flux is going into maintenance mode. You can continue using it as you currently are without any changes to your code.

Read more

InfluxDB 3 Core and Enterprise are now in Beta

InfluxDB 3 Core and Enterprise are now available for beta testing, available under MIT or Apache 2 license.

InfluxDB 3 Core is a high-speed, recent-data engine that collects and processes data in real-time, while persisting it to local disk or object storage. InfluxDB 3 Enterprise is a commercial product that builds on Core’s foundation, adding high availability, read replicas, enhanced security, and data compaction for faster queries. A free tier of InfluxDB 3 Enterprise will also be available for at-home, non-commercial use for hobbyists to get the full historical time series database set of capabilities.

For more information, check out: