Contact Us 1-800-596-4880

toDateTime

toDateTime(str: String, formatters: Array<Formatter>): DateTime

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

formatters

Array<Formatter>

The array of formatting to use on the DateTime value.

Example

This example shows how toDateTime 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: toDateTime("2003-10-01 23:57:59Z", [{format: "uuuu/MM/dd HH:mm:ssz"}, {format: "uuuu-MM-dd HH:mm:ssz"}]),
  b: try(() -> toDateTime("2003-10-01 23:57:59Z", [{format: "uuuu/MM/dd HH:mm:ssz"}])).error.message
}

Output

{
  a: |2003-10-01T23:57:59Z| as DateTime {format: "uuuu-MM-dd HH:mm:ssz"},
  b: "Could not find a valid formatter for '2003-10-01 23:57:59Z'"
}

toDateTime(number: Number, unit: MillisOrSecs | Null = null): DateTime

Transforms a Number value into a DateTime value using milliseconds or seconds as the unit.

Introduced in DataWeave version 2.4.0.

Parameters

Name Description

number

The Number value to transform into a DateTime value.

unit

The unit to use for the conversion: "milliseconds" or "seconds". A null value for the unit field defaults to "seconds".

Example

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

Source

%dw 2.0
import * from dw::util::Coercions
output application/dw
---
{
    fromEpoch: toDateTime(1443743879),
    fromMillis: toDateTime(1443743879000, "milliseconds")
}

Output

{
  fromEpoch: |2015-10-01T23:57:59Z|,
  fromMillis: |2015-10-01T23:57:59Z| as DateTime {unit: "milliseconds"}
}

toDateTime(str: String, format: String | Null = null, locale: String | Null = null): DateTime

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

format

The formatting to use on the DateTime value. A null value has no effect on the DateTime value. This parameter accepts Java character patterns based on ISO-8601. A DateTime value, such as 2011-12-03T10:15:30.000000+01:00, has the format uuuu-MM-dd HH:mm:ssz.

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 toDateTime 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: toDateTime("2015-10-01T23:57:59Z"),
   b: toDateTime("2003-10-01 23:57:59Z","uuuu-MM-dd HH:mm:ssz")
}

Output

{
  a: |2015-10-01T23:57:59Z|,
  b: |2003-10-01T23:57:59Z| as DateTime {format: "uuuu-MM-dd HH:mm:ssz"}
}