# Cannot change Date from String into DateTime format

**URL:** https://discourse.nodered.org/t/cannot-change-date-from-string-into-datetime-format/79869
**Category:** General
**Created:** [17 July 2023 03:25 UTC](https://discourse.nodered.org/t/cannot-change-date-from-string-into-datetime-format/79869 "2023-07-17T03:25:23Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![grant1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/grant1/32/49890_2.png) [@grant1](https://discourse.nodered.org/u/grant1)
#### Post date: [17 July 2023 03:25 UTC](https://discourse.nodered.org/t/cannot-change-date-from-string-into-datetime-format/79869/1 "2023-07-17T03:25:23Z")

</div>

Hello. I am querying an API ([eia.gov](http://eia.gov)) to get hourly electricity usage for certain entities (called "respondents") in the US. I got the HTTP request working fine, and a typical output is an array with 6 objects like this:

```auto
[{"period":"2023-07-07T00","respondent":"NYIS","respondent-name":"New York Independent System Operator","type":"D","type-name":"Demand","value":27010,"value-units":"megawatthours"},{"period":"2023-07-07T00","respondent":"CISO","respondent-name":"California Independent System Operator","type":"D","type-name":"Demand","value":26255,"value-units":"megawatthours"},{"period":"2023-07-07T00","respondent":"CISO","respondent-name":"California Independent System Operator","type":"TI","type-name":"Total interchange","value":3691,"value-units":"megawatthours"},{"period":"2023-07-07T00","respondent":"CISO","respondent-name":"California Independent System Operator","type":"NG","type-name":"Net generation","value":31689,"value-units":"megawatthours"},{"period":"2023-07-07T00","respondent":"NYIS","respondent-name":"New York Independent System Operator","type":"TI","type-name":"Total interchange","value":-1293,"value-units":"megawatthours"},{"period":"2023-07-07T00","respondent":"NYIS","respondent-name":"New York Independent System Operator","type":"NG","type-name":"Net generation","value":25717,"value-units":"megawatthours"}]

```

I then pass the above array through a function node that uses an array.map function:

```auto
const data = msg.payload.response.data;

// Extract the 4 desired fields using array.map
const extractedData = data.map(item => {
    return {
        period: item.period,
        respondent: item.respondent,
        type: item.type,
        value: item.value
    };
});

// Set the extracted data as the new payload
msg.payload = extractedData;

return msg;

```

Everything is fine except for the fact that the date (what is referred to as "period" representing a given hour of a given day) is being returned as a string...

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/3/c34b45b627cefb05f92dcbbf56ed2030bd68ce29.png)

and I want it to be DateTime format (or better, the epoch time format). I tried following [this thread](https://discourse.nodered.org/t/how-to-change-date-from-string-into-datetime-format/25013) to use `Date.parse()` and `new Date()` in my function node above, but these gave me `NaN`. I have to believe it is something simple that I have overlooked.

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [17 July 2023 04:04 UTC](https://discourse.nodered.org/t/cannot-change-date-from-string-into-datetime-format/79869/2 "2023-07-17T04:04:37Z")

</div>

> [@grant1](#):
>
> `period: item.period`

Try `period: new Date(item.period.split("T")[0])`

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [17 July 2023 08:29 UTC](https://discourse.nodered.org/t/cannot-change-date-from-string-into-datetime-format/79869/3 "2023-07-17T08:29:00Z")

</div>

Assuming that the "T00" changes to "T01", etc. once you have split the string, the 2nd element will contain the hour number and you could add that to the date. Have a look on the Mozilla Developer Network site for the `Date` class and you will find some functions for doing that.

---

<div class="post-metadata">

### Author: ![grant1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/grant1/32/49890_2.png) [@grant1](https://discourse.nodered.org/u/grant1)
#### Post date: [17 July 2023 09:21 UTC](https://discourse.nodered.org/t/cannot-change-date-from-string-into-datetime-format/79869/4 "2023-07-17T09:21:59Z")

</div>

Thanks for the hints and advice. Got it working with this:

```auto
period: Date.parse(new Date(item.period.split("T")[0]))

```

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/c/1c1cbf45171b8cdbd3a09275894090c0a84afebe.png)

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [17 July 2023 11:08 UTC](https://discourse.nodered.org/t/cannot-change-date-from-string-into-datetime-format/79869/5 "2023-07-17T11:08:49Z")

</div>

> [@grant1](#):
>
> `Date.parse`

Date.parse is redundant here. The solution I gave you should be all you need.

---

<div class="post-metadata">

### Author: ![grant1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/grant1/32/49890_2.png) [@grant1](https://discourse.nodered.org/u/grant1)
#### Post date: [17 July 2023 11:36 UTC](https://discourse.nodered.org/t/cannot-change-date-from-string-into-datetime-format/79869/6 "2023-07-17T11:36:35Z")

</div>

> [@Steve-Mcl](#):
>
> Try `period: new Date(item.period.split("T")[0])`

The above did not convert it to datetime. It remained as a string:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/7/87de3c92bed64be2f05fb4ddfadaf59b87ec1d9d.png)

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [17 July 2023 11:48 UTC](https://discourse.nodered.org/t/cannot-change-date-from-string-into-datetime-format/79869/7 "2023-07-17T11:48:44Z")

</div>

> [@grant1](#):
>
> The above did not convert it to datetime.

Actually, that is exactly what it did. The debug representation of nested properties does not tell you the type. In fact it sees a date object and calls toString - just for you - trying to be friendly (it remains a date object)

What you did does NOT return a datetime.  
[`Date.parse`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse) return the epoch (a number, not a string)

You can (should) prove this for yourself

Add the below to your code...

```auto
node.warn({ 
  item_period: item.period,
  item_period_type: typeof item.period,
  item_period_new_date: new Date(item.period.split("T")[0]),
  item_period_new_date_type: typeof (new Date(item.period.split("T")[0])),
  item_period_new_date_parse: Date.parse(new Date(item.period.split("T")[0])),
  item_period_new_date_parse_type: typeof Date.parse(new Date(item.period.split("T")[0]))
})

```

---

<div class="post-metadata">

### Author: ![grant1](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/grant1/32/49890_2.png) [@grant1](https://discourse.nodered.org/u/grant1)
#### Post date: [17 July 2023 12:53 UTC](https://discourse.nodered.org/t/cannot-change-date-from-string-into-datetime-format/79869/8 "2023-07-17T12:53:53Z")

</div>

Got it...Thank you for the explanation. I just assumed " " in the debug pane meant it was a string.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [31 July 2023 12:54 UTC](https://discourse.nodered.org/t/cannot-change-date-from-string-into-datetime-format/79869/9 "2023-07-31T12:54:05Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
