Cannot change Date from String into DateTime format

Hello. I am querying an API (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:

[{"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:

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

and I want it to be DateTime format (or better, the epoch time format). I tried following this thread 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.

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

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.

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

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

image

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

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

image

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 return the epoch (a number, not a string)

You can (should) prove this for yourself

Add the below to your code...

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]))
})

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

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