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

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