# Help making datetime conversion more user friendly

**URL:** https://discourse.nodered.org/t/help-making-datetime-conversion-more-user-friendly/87705
**Category:** General
**Tags:** function-node
**Created:** [4 May 2024 16:59 UTC](https://discourse.nodered.org/t/help-making-datetime-conversion-more-user-friendly/87705 "2024-05-04T16:59:35Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![mikefila](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mikefila/32/95223_2.png) [@mikefila](https://discourse.nodered.org/u/mikefila)
#### Post date: [4 May 2024 16:59 UTC](https://discourse.nodered.org/t/help-making-datetime-conversion-more-user-friendly/87705/1 "2024-05-04T16:59:35Z")

</div>

I have a flow to process calendar events from [this scrape](https://raw.githubusercontent.com/bigfoott/ScrapedDuck/data/events.min.json). It will return a local datetime in the form of YYYY-MM-DD HH:mm:ss.

This works for me but limited to "en-US" without modification. Input is ISO

```auto
let start = new Date(msg.payload).toLocaleString("en-US", { hour12: false });

let x = start.split(","); //split date and time
let y = x[0].split("/"); // split date
let z = `${y[2]}-${y[1]}-${y[0]}` + x[1]; //new string YYYY-MM-DD HH:mm:ss

msg.payload = z;
return msg;

```

```auto
[{"id":"a9e4921e7027d964","type":"debug","z":"0a325c35fc29f44e","name":"debug 179","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":650,"y":400,"wires":[]},{"id":"f5dbee9d71387bf9","type":"function","z":"0a325c35fc29f44e","name":"function 18","func":"let start = new Date(msg.payload).toLocaleString(\"en-US\", { hour12: false });\n\nlet x = start.split(\",\"); //split date and time\nlet y = x[0].split(\"/\"); // split date\nlet z = `${y[2]}-${y[1]}-${y[0]}` + x[1]; //new string YYYY-MM-DD HH:mm:ss\n\nmsg.payload = z;\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":410,"y":400,"wires":[["a9e4921e7027d964"]]},{"id":"e971c76b5dc4198c","type":"inject","z":"0a325c35fc29f44e","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"2024-04-26T20:00:00.000Z","payloadType":"str","x":240,"y":400,"wires":[["f5dbee9d71387bf9"]]}]

```

I would like to make the code I am using universal so that regardless of the users tz, the datetime is converted to local time in the proper format.

Thanks,  
Mike

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [4 May 2024 21:30 UTC](https://discourse.nodered.org/t/help-making-datetime-conversion-more-user-friendly/87705/2 "2024-05-04T21:30:27Z")

</div>

It may be easier to do this using a change node;

set msg.payload to

```auto
$moment(payload).tz('America/Los_Angeles').format("YYYY-MM-DD HH:mm:ss")

// input payload: 2024-05-04T10:00:00.000
// output payload: 2024-05-04 01:00:00

```

---

<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: [4 May 2024 21:32 UTC](https://discourse.nodered.org/t/help-making-datetime-conversion-more-user-friendly/87705/3 "2024-05-04T21:32:41Z")

</div>

If you don't want to handle things yourself, try out either the moment node or the $moment function in JSONata.

But there is generally no real need to do locale conversions in Node-RED itself, leave that to the browser. So process and store everything on the server end in UTC and convert to/from local when interacting with real people. It avoids a shed-load of hassle.

---

<div class="post-metadata">

### Author: ![mikefila](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mikefila/32/95223_2.png) [@mikefila](https://discourse.nodered.org/u/mikefila)
#### Post date: [4 May 2024 23:11 UTC](https://discourse.nodered.org/t/help-making-datetime-conversion-more-user-friendly/87705/4 "2024-05-04T23:11:01Z")

</div>

The output is being pushed into a calendar and there is no conversion, I have to send a converted date. In the scrape there are 13 different event types with several sub types that mostly need separate processing.

It's easier for me to handle the information in a function node rather than a fan of 13+ flows. I already have a basic function working but only for my locale. Eventually I'd like to be able to share it without the need to modify it.

Is there away to get a users tz from an environmental variable? If I could get the offset then make the adjustment to the UTC time. Send that `toISOString()` and strip `T` and `Z` from it.

---

<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: [4 May 2024 23:57 UTC](https://discourse.nodered.org/t/help-making-datetime-conversion-more-user-friendly/87705/5 "2024-05-04T23:57:20Z")

</div>

> [@mikefila](#):
>
> function node

Moment should be available in the fn node as well in newer versions of Node-RED, I keep forgetting that.

> [@mikefila](#):
>
> Is there away to get a users tz from an environmental variable? If I could get the offset then make the adjustment to the UTC time. Send that `toISOString()` and strip `T` and `Z` from it.

Certainly. This article shows how:

[https://www.cyberciti.biz/faq/linux-unix-set-tz-environment-variable/](https://www.cyberciti.biz/faq/linux-unix-set-tz-environment-variable/)

---

<div class="post-metadata">

### Author: ![mikefila](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mikefila/32/95223_2.png) [@mikefila](https://discourse.nodered.org/u/mikefila)
#### Post date: [5 May 2024 02:56 UTC](https://discourse.nodered.org/t/help-making-datetime-conversion-more-user-friendly/87705/6 "2024-05-05T02:56:28Z")

</div>

Idk how many times I've been through js date methods and hadn't noticed `getTimezoneOffset`. Thanks for the help. My ruff final solution

```auto
let d1 = new Date(msg.payload);
let x = d1.getTime(); // convert payload to ms

let d2 = new Date();
let y = d2.getTimezoneOffset() * 60 * 1000; // get timezone offset to ms

let z = x - y; //tz adjust
let tt = new Date(z).toISOString(); // convert to ISO date
msg.payload = tt.replace("T", " ").replace(".000Z", "");

return msg;

```

---

<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: [5 May 2024 16:01 UTC](https://discourse.nodered.org/t/help-making-datetime-conversion-more-user-friendly/87705/7 "2024-05-05T16:01:05Z")

</div>

😀

Easy to miss for sure.

And if you are updating your knowledge, check out the INTL library as well as it provides a lot of date/time processing that you needed libraries for in the past, especially if you are using node.js v18+

---

<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: [3 August 2024 16:01 UTC](https://discourse.nodered.org/t/help-making-datetime-conversion-more-user-friendly/87705/8 "2024-08-03T16:01:56Z")

</div>

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