Skip to content

parseTimestamp

Description

Returns a parsed timestamp from a date or time string, enabling use of DataPrime's time functions.

Note

If the string cannot be parsed (for example, if it does not match the expected format), the function returns null.

Syntax

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

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

Arguments

NameTypeRequiredDescription
stringstringtrueThe string to parse into a timestamp
formatstringfalseThe format of the input string. Defaults to auto, which attempts to match against well-known formats.
tzstringfalseA valid time zone string

Example 1

Use case: Parse a simple date string using default format

Example query

choose parseTimestamp('2023-04-05') as ts
choose '2023-04-05'.parseTimestamp() as ts

Example output

{
    "ts": 1680652800000000000
}

Example 2

Use case: Parse a US-style date

Example query

choose parseTimestamp('04/05/23', '%D') as ts
choose '04/05/23'.parseTimestamp('%D') as ts

Example output

{
    "ts": 1680652800000000000
}

Example 3

Use case 3: Parse date and time with units

Example query

choose parseTimestamp('2023-04-05 16h07m', '%F %Hh%Mm') as ts
choose '2023-04-05 16h07m'.parseTimestamp('%F %Hh%Mm') as ts

Example output

{
    "ts": 1680710820000000000
}

Example 4

Use case 4: Parse a Unix timestamp in seconds

Example query

choose parseTimestamp('1680710853', 'timestamp_second') as ts
choose '1680710853'.parseTimestamp('timestamp_second') as ts

Example output

{
    "ts": 1680710853000000000
}

Example 5

Use case 5: Parse inconsistent formats with multiple options

Some logs contain timestamps in different formats. Provide a |-separated list of formats to handle them all.

Example data

{ "ts": "1680710853123", "app": "app1" },
{ "ts": "2023-04-05", "app": "app2" },
{ "ts": "2023-04-05T16:07:33.123Z", "app": "app3" }

Example query

create ts_parsed from parseTimestamp(ts, 'timestamp_second|%Y-%m-%d|iso8601')
create ts_parsed from ts.parseTimestamp('timestamp_second|%Y-%m-%d|iso8601')

Example output

{ "ts": "1680710853123", "app": "app1", "ts_parsed": 1680710853123000000 },
{ "ts": "2023-04-05", "app": "app2", "ts_parsed": 1680652800000000000 },
{ "ts": "2023-04-05T16:07:33.123Z", "app": "app3", "ts_parsed": 1680710853123000000 }