---
schema_version: "1"
id: de8d48806ed946b3173e912851a38d0d8acfeb10a7d483755cb185b5484280b5
title: toDateTime
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/2.4/dw-coercions-functions-todatetime.md
canonical_url: https://docs.mulesoft.com/dataweave/2.4/dw-coercions-functions-todatetime
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 38f0914326b0743d6af294b15782921a166fe45cc0d3642f2c3bdf5ded578e8e
description: MuleSoft Documentation Site
release: "2.4"
latest: false
---
---
product: DataWeave
version: 2.4
---

# toDateTime

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

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

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>number</code></p></td><td><p>The <code>Number</code> value to transform into a <code>DateTime</code> value.</p></td></tr><tr><td><p><code>unit</code></p></td><td><p>The unit to use for the conversion: <code>"milliseconds"</code> or <code>"seconds"</code>. A <code>null</code> value for the <code>unit</code> field defaults to <code>"seconds"</code>.</p></td></tr></tbody></table>

### Example

This example shows how `toDateTime` 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
---
{
    fromEpoch: toDateTime(1443743879),
    fromMillis: toDateTime(1443743879000, "milliseconds")
}
```

#### Output

```DataWeave
{
  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

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>str</code></p></td><td><p>The <code>String</code> value to transform into a <code>DateTime</code> value.</p></td></tr><tr><td><p><code>format</code></p></td><td><p>The formatting to use on the <code>DateTime</code> value. A <code>null</code> value has no effect on the <code>DateTime</code> value. This parameter accepts Java character patterns based on ISO-8601. A <code>DateTime</code> value, such as <code>2011-12-03T10:15:30.000000+01:00</code>, has the format <code>uuuu-MM-dd HH:mm:ssz</code>.</p></td></tr><tr><td><p><code>locale</code></p></td><td><p>Optional ISO 3166 country code to use, such as <code>US</code>, <code>AR</code>, or <code>ES</code>. A <code>null</code> or absent value uses your JVM default.</p></td></tr></tbody></table>

### Example

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

#### Output

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