Documentation

Work with durations

A duration type represents a length of time with nanosecond precision.

Type name: duration

Duration syntax

Duration literals contain integers and unit specifiers. Flux supports the following unit specifiers:

  • ns: nanosecond
  • us: microsecond
  • ms: millisecond
  • s: second
  • m: minute
  • h: hour
  • d: day
  • w: week
  • mo: calendar month
  • y: calendar year
1ns // 1 nanosecond
1us // 1 microsecond
1ms // 1 millisecond
1s  // 1 second
1m  // 1 minute
1h  // 1 hour
1d  // 1 day
1w  // 1 week
1mo // 1 calendar month
1y  // 1 calendar year

3d12h4m25s // 3 days, 12 hours, 4 minutes, and 25 seconds

Do not include leading zeros in duration literals

The integer part of a duration literal should not contain leading zeros. Leading zeros are parsed as separate integer literals. For example:

01m // parsed as 0 (integer literal) and 1m (duration literal)
02h05m // parsed as 0 (integer literal), 2h (duration literal), 0 (integer literal), and 5m (duration literal)

Convert data types to durations

Use the duration() function to convert the following basic types to durations:

  • string: parsed as a duration string and converted to a duration.
  • int: parsed as nanoseconds and converted to a duration.
  • uint: parsed as nanoseconds and converted to a duration.
duration(v: "1h30m")
// Returns 1h30m

duration(v: 1000000)
// Returns 1ms

duration(v: uint(v: 3000000000))
// Returns 3s

Flux does not support duration columns.

Operate on durations

Perform arithmetic operations on durations

To perform operations like adding, subtracting, multiplying, or dividing duration values:

  1. Use int() or uint() to convert duration values to numeric values.
  2. Use arithmetic operators to operate on numeric values.
  3. Use duration() to convert the calculated numeric value into a duration.
duration(v: int(v: 6h4m) + int(v: 22h32s))
// Returns 1d4h4m32s

duration(v: int(v: 22h32s) - int(v: 6h4m))
// Returns 15h56m32s

duration(v: int(v: 32m10s) * 10)
// Returns 5h21m40s

duration(v: int(v: 24h) / 2)
// Returns 12h

Add a duration to a time value

  1. Import the date package.
  2. Use date.add() to add a duration to a time value.
import "date"

date.add(d: 1w, to: 2021-01-01T00:00:00Z)
// Returns 2021-01-08T00:00:00.000000000Z

Subtract a duration from a time value

  1. Import the date package.
  2. Use date.sub() to subtract a duration from a time value.
import "date"

date.sub(d: 1w, from: 2021-01-01T00:00:00Z)
// Returns 2020-12-25T00:00:00.000000000Z

Was this page helpful?

Thank you for your feedback!


Introducing InfluxDB Clustered

A highly available InfluxDB 3.0 cluster on your own infrastructure.

InfluxDB Clustered is a highly available InfluxDB 3.0 cluster built for high write and query workloads on your own infrastructure.

InfluxDB Clustered is currently in limited availability and is only available to a limited group of InfluxData customers. If interested in being part of the limited access group, please contact the InfluxData Sales team.

Learn more
Contact InfluxData Sales

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.

Flux is going into maintenance mode and will not be supported in InfluxDB 3.0. This was a decision based on the broad demand for SQL and the continued growth and adoption of InfluxQL. We are continuing to support Flux for users in 1.x and 2.x so you can continue using it with no changes to your code. If you are interested in transitioning to InfluxDB 3.0 and want to future-proof your code, we suggest using InfluxQL.

For information about the future of Flux, see the following: