---
schema_version: "1"
id: c2662c1cf32ea1ee9a6e88c70a7aec8642ae7d8a7023d4c153f9d8680fd6d456
title: toDateTimeOrNull
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/2.6/dw-coercions-functions-todatetimeornull.md
canonical_url: https://docs.mulesoft.com/dataweave/2.6/dw-coercions-functions-todatetimeornull
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 4d7a57b7746025c854a620c0f60b58fd702e65a2f054577e7489a4a15db1fb11
description: MuleSoft Documentation Site
release: "2.6"
latest: false
---
---
product: DataWeave
version: 2.6
---

# toDateTimeOrNull

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

## toDateTimeOrNull(str: String, formatters: Array<Formatter>): DateTime | Null

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

If none of the `Formatter` matches with the given value, the function returns a `null` value.

_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 `toDateTimeOrNull` 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: toDateTimeOrNull("2003-10-01 23:57:59Z", [{format: "uuuu/MM/dd HH:mm:ssz"}, {format: "uuuu-MM-dd HH:mm:ssz"}]),
  b: toDateTimeOrNull("2003-10-01 23:57:59Z", [{format: "uuuu/MM/dd HH:mm:ssz"}])
}
```

#### Output

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