Skip to content

formatTimestamp - Writes out a timestamp as a formatted string

formatTimestamp formats a timestamp to a string with an optional format specification and destination 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 to render the timestamp. Defaults to iso8601
tzstringfalseMust be a valid Time Zone string. See Time Zone section to find out more.

Valid format values

The format argument can be represented by a number of values. Some examples are:

  • '%Y-%m-%d' - print the date only, e.g. '2023-04-05'
  • '%H:%M:%S' - print the time only, e.g. '16:07:33'
  • '%F %H:%M:%S' - print both date and time, e.g. '2023-04-05 16:07:33'
  • 'iso8601' - print a timestamp in ISO 8601 format, e.g. '2023-04-05T16:07:33.123Z'
  • 'timestamp_milli' - print a timestamp in milliseconds (13 digits), e.g. '1680710853123'

Example - print a timestamp with default format and +5h offset

choose formatTimestamp($m.timestamp, tz='+05') as ts # Result 1: { "ts": "2023-08-29T19:08:37.405937400+0500" }

choose $m.timestamp.formatTimestamp(tz='+05') as ts # Result 1: { "ts": "2023-08-29T19:08:37.405937400+0500" }

Example - print only the year and month

choose formatTimestamp($m.timestamp, '%Y-%m') as ym # Result 2: { "ym": "2023-08" }

choose $m.timestamp.formatTimestamp('%Y-%m') as ym # Result 2: { "ym": "2023-08" } 

Example - print only the hours and minutes

choose formatTimestamp($m.timestamp, '%H:%M') as hm # Result 3: { "hm": "14:11" }
choose $m.timestamp.formatTimestamp('%H:%M') as hm # Result 3: { "hm": "14:11" }

Example - print a timestamp in milliseconds (13 digits)

choose formatTimestamp($m.timestamp, 'timestamp_milli') as ms # Result 4: { "ms": "1693318678696" }

choose $m.timestamp.formatTimestamp('timestamp_milli') as ms # Result 4: { "ms": "1693318678696" }