# `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](https://coralogix.com/docs/dataprime/language-reference/functions-reference/index.md), **function** and **method** notation. These interchangeable forms allow flexibility in how you structure expressions.

```dataprime
parseTimestamp(string: string, format?: string, tz?: string): timestamp
```

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

## Arguments

| Name   | Type   | Required  | Description                                                                                             |
| ------ | ------ | --------- | ------------------------------------------------------------------------------------------------------- |
| string | string | **true**  | The string to parse into a timestamp                                                                    |
| format | string | **false** | The format of the input string. Defaults to `auto`, which attempts to match against well-known formats. |
| tz     | string | **false** | A valid time zone string                                                                                |

## Example 1

**Use case: Parse a simple date string using default format**

### Example query

```dataprime
choose parseTimestamp('2023-04-05') as ts
```

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

### Example output

```json
{
    "ts": 1680652800000000000
}
```

## Example 2

**Use case: Parse a US-style date**

### Example query

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

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

### Example output

```json
{
    "ts": 1680652800000000000
}
```

## Example 3

**Use case 3: Parse date and time with units**

### Example query

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

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

### Example output

```json
{
    "ts": 1680710820000000000
}
```

## Example 4

**Use case 4: Parse a Unix timestamp in seconds**

### Example query

```dataprime
choose parseTimestamp('1680710853', 'timestamp_second') as ts
```

```dataprime
choose '1680710853'.parseTimestamp('timestamp_second') as ts
```

### Example output

```json
{
    "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

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

### Example query

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

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

### Example output

```json
{ "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 }
```
