---
schema_version: "1"
id: 6f32ac87041f93d55f835ae32147691871504566f9a898a4a9eeba957311b456
title: toDateTime
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/latest/dw-coercions-functions-todatetime.md
canonical_url: https://docs.mulesoft.com/dataweave/latest/dw-coercions-functions-todatetime
source_site: docs.mulesoft.com
content_type: documentation
content_hash: d4deed3fba8b1ac5cbe50e4ced087cd6e87b8f31fa67e218e489f37947d0b84f
description: MuleSoft Documentation Site
release: "2.13"
latest: true
---
---
product: DataWeave
version: 2.13
is-latest-version: true
---

# toDateTime

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

## toDateTime(str: String, formatters: Array<Formatter>): DateTime

Transforms a `String` value into a `DateTime` value using the first `Formatter` that matches with
the given value to transform.

_Introduced in DataWeave version 2.5.0._

### Parameters

<table><colgroup><col> <col> <col></colgroup><thead><tr><th>Name</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td><p><code>str</code></p></td><td><p>String</p></td><td><p>The <code>String</code> value to transform into a <code>DateTime</code> value.</p></td></tr><tr><td><p><code>formatters</code></p></td><td><p>Array&lt;Formatter&gt;</p></td><td><p>The <code>array</code> of formatting to use on the <code>DateTime</code> value.</p></td></tr></tbody></table>

### Example

This example shows how `toDateTime` behaves with different inputs. It produces output in the
`application/dw` format.

#### Source

```DataWeave
%dw 2.0
import * from dw::util::Coercions
import * from dw::Runtime
output application/dw
---
{
  a: toDateTime("2003-10-01 23:57:59Z", [{format: "uuuu/MM/dd HH:mm:ssz"}, {format: "uuuu-MM-dd HH:mm:ssz"}]),
  b: try(() -> toDateTime("2003-10-01 23:57:59Z", [{format: "uuuu/MM/dd HH:mm:ssz"}])).error.message
}
```

#### Output

```DataWeave
{
  a: |2003-10-01T23:57:59Z| as DateTime {format: "uuuu-MM-dd HH:mm:ssz"},
  b: "Could not find a valid formatter for '2003-10-01 23:57:59Z'"
}
```

## toDateTime(number: Number, unit: MillisOrSecs | Null = null): DateTime

Transforms a `Number` value into a `DateTime` value using `milliseconds` or `seconds` as the unit.

_Introduced in DataWeave version 2.4.0._

### Parameters

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>number</code></p></td><td><p>The <code>Number</code> value to transform into a <code>DateTime</code> value.</p></td></tr><tr><td><p><code>unit</code></p></td><td><p>The unit to use for the conversion: <code>"milliseconds"</code> or <code>"seconds"</code>. A <code>null</code> value for the <code>unit</code> field defaults to <code>"seconds"</code>.</p></td></tr></tbody></table>

### Example

This example shows how `toDateTime` behaves with different inputs. It produces output in the
`application/dw` format.

#### Source

```DataWeave
%dw 2.0
import * from dw::util::Coercions
output application/dw
---
{
    fromEpoch: toDateTime(1443743879),
    fromMillis: toDateTime(1443743879000, "milliseconds")
}
```

#### Output

```DataWeave
{
  fromEpoch: |2015-10-01T23:57:59Z|,
  fromMillis: |2015-10-01T23:57:59Z| as DateTime {unit: "milliseconds"}
}
```

## toDateTime(str: String, format: String | Null = null, locale: String | Null = null): DateTime

Transforms a `String` value into a `DateTime` value and accepts a format and locale.

_Introduced in DataWeave version 2.4.0._

### Parameters

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>str</code></p></td><td><p>The <code>String</code> value to transform into a <code>DateTime</code> value.</p></td></tr><tr><td><p><code>format</code></p></td><td><p>The formatting to use on the <code>DateTime</code> value. A <code>null</code> value has no effect on the <code>DateTime</code> value. This parameter accepts Java character patterns based on ISO-8601. A <code>DateTime</code> value, such as <code>2011-12-03T10:15:30.000000+01:00</code>, has the format <code>uuuu-MM-dd HH:mm:ssz</code>.</p></td></tr><tr><td><p><code>locale</code></p></td><td><p>Optional ISO 3166 country code to use, such as <code>US</code>, <code>AR</code>, or <code>ES</code>. A <code>null</code> or absent value uses your JVM default.</p></td></tr></tbody></table>

### Example

This example shows how `toDateTime` behaves with different inputs. It produces output in the
`application/dw` format.

#### Source

```DataWeave
%dw 2.0
import * from dw::util::Coercions
output application/dw
---
{
   a: toDateTime("2015-10-01T23:57:59Z"),
   b: toDateTime("2003-10-01 23:57:59Z","uuuu-MM-dd HH:mm:ssz")
}
```

#### Output

```DataWeave
{
  a: |2015-10-01T23:57:59Z|,
  b: |2003-10-01T23:57:59Z| as DateTime {format: "uuuu-MM-dd HH:mm:ssz"}
}
```
