Contact Us 1-800-596-4880

toDateOrNull

toDateOrNull(str: String, formatters: Array<Formatter>): Date | Null

Transforms a String value into a Date 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

Name Type Description

str

String

The String value to transform into a Date value.

formatters

Array<Formatter>

The array of formatting to use on the Date value.

Example

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

Source

%dw 2.0
import * from dw::util::Coercions
output application/dw
---
{
  a: toDateOrNull("2023-28-03", [{format: "yyyy/MM/dd"}, {format: "yyyy-dd-MM", locale: "en_US"}]),
  b: toDateOrNull("2023-28-03", [{format: "yyyy/MM/dd"}])
}

Output

{
  a: |2023-03-28| as Date {format: "yyyy-dd-MM", locale: "en_US"},
  b: null
}