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

# 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
}
```
