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. These interchangeable forms allow flexibility in how you structure expressions.
- Function notation
- Method notation
formatTimestamp(timestamp: timestamp, format?: string, tz?: string): string
(timestamp: timestamp).formatTimestamp(format?: string, tz?: string): string
Arguments
| Name | Type | Required | Description |
|---|---|---|---|
timestamp | timestamp | true | The timestamp to format |
format | string | false | The format specification. Defaults to iso8601 |
tz | string | false | A 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
- Function notation
- Method notation
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
- Function notation
- Method notation
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
- Function notation
- Method notation
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
- Function notation
- Method notation
choose formatTimestamp($m.timestamp, 'timestamp_milli') as ms
choose $m.timestamp.formatTimestamp('timestamp_milli') as ms
Example output
{
"ms": "1693318678696"
}