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

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