Contact Us 1-800-596-4880

toDate

toDate(str: String, formatters: Array<Formatter>): Date

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

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 toDate behaves with different inputs. It produces output in the application/dw format.

Source

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

Output

{
  a: |2023-03-28| as Date {format: "yyyy-dd-MM", locale: "en_US"},
  b: "Could not find a valid formatter for '2023-28-03'"
}

toDate(str: String, format: String | Null = null, locale: String | Null = null): Date

Transforms a String value into a Date value and accepts a format and locale.

Introduced in DataWeave version 2.4.0.

Parameters

Name Description

str

The String value to transform into a Date value.

format

The formatting to use on the Date value. A null value has no effect on the Date value. This parameter accepts Java character patterns based on ISO-8601. A Date value, such as 2011-12-03, has the format uuuu-MM-dd.

locale

Optional ISO 3166 country code to use, such as US, AR, or ES. A null or absent value uses your JVM default.

Example

This example shows how toDate 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: toDate("2015-10-01"),
  b: toDate("2003/10/01","uuuu/MM/dd")
}

Output

{
  a: |2015-10-01|,
  b: |2003-10-01| as Date {format: "uuuu/MM/dd"}
}