Skip to content

formatTimestamp

Description

Returns a timestamp formatted as a string, with optional control over the output format and time zone.

Syntax

Like many functions in DataPrime, formatTimestamp supports two notations, function and method notation. These interchangeable forms allow flexibility in how you structure expressions.

formatTimestamp(timestamp: timestamp, format?: string, tz?: string): string
(timestamp: timestamp).formatTimestamp(format?: string, tz?: string): string

Arguments

NameTypeRequiredDescription
timestamptimestamptrueThe timestamp to format
formatstringfalseThe format specification. Defaults to iso8601
tzstringfalseA valid time zone string (see Time Zone section for details)

Example 1

Print a timestamp with a +5h offset using the default format**

Example query

choose formatTimestamp($m.timestamp, tz='+05') as ts
choose $m.timestamp.formatTimestamp(tz='+05') as ts

Example output

{
    "ts": "2023-08-29T19:08:37.405937400+0500"
}

Example 2

Use case 2: Print only the year and month

Example query

choose formatTimestamp($m.timestamp, '%Y-%m') as ym
choose $m.timestamp.formatTimestamp('%Y-%m') as ym

Example output

{
    "ym": "2023-08"
}

Example 3

Use case 3: Print only the hours and minutes

Example query

choose formatTimestamp($m.timestamp, '%H:%M') as hm
choose $m.timestamp.formatTimestamp('%H:%M') as hm

Example output

{
"hm": "14:11"
}

Example 4

Use case 4: Print a timestamp in milliseconds (13 digits)

Example query

choose formatTimestamp($m.timestamp, 'timestamp_milli') as ms
choose $m.timestamp.formatTimestamp('timestamp_milli') as ms

Example output

{
    "ms": "1693318678696"
}