# Understanding how to print time in local timezone

**URL:** <https://discourse.nodered.org/t/understanding-how-to-print-time-in-local-timezone/90797>\
**Category:** General\
**Created:** [8 September 2024 21:09 UTC](https://discourse.nodered.org/t/understanding-how-to-print-time-in-local-timezone/90797 "2024-09-08T21:09:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nygma2004](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nygma2004/32/1308_2.png) [@nygma2004](https://discourse.nodered.org/u/nygma2004)\
**Post date:** [8 September 2024 21:09 UTC](https://discourse.nodered.org/t/understanding-how-to-print-time-in-local-timezone/90797/1 "2024-09-08T21:09:30Z")

</div>

I thought I understand how datetime works in Javascript, but I sometimes I run into issues and I question myself over-and-over again. I know that the timestamp is stored in UTC and we should only do a conversion to local timezone when we want to print the information on the screen. And I know the timezone on the machine where Node-Red is running should be set correctly according to the local timezone.

I am working on machine in Finland, Node-Red is running on an IoT gateway also in Findland which is configured like this:

 ![Screenshot 2024-09-08 224745](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/d/8da19b0b6eac113701fccf5334733b02b5784b73.png)

I assume this is all correct, since Finland in UTC+3 and GMT+2 because of the DST now.

If I do a simple `toLocaleString` it just converts the timestamp into a time format, in this case UK but the time is the UTC time:

 ![Screenshot 2024-09-08 225034](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/8/a81b7468c5897a5bc7445904f78683fccc5da974.png)  
The local time is 23:50 as shown currectly first header of the debug message, but I assume this only validates that the browser is running in the correct local timezone.

Now if I print the `getTimezoneOffset`, I get -180 which is the UTC offset in the Summer (DST active).

 ![Screenshot 2024-09-08 225051](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/5/35450e33f6e8775578b9be3896e8eabc7780b79b.png)  
That again is correct, so it means that Node-Red knows that the local timezone is Finland.

I created this simple function to convert the UTC time to Finnish local time:

 ![Screenshot 2024-09-08 225110](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/2/e2c123eb6a10ffe204f4b772dc0dfc2d673233f9.png)

This code works, as I get the time in local time, and matches the time in the computer I work on. Everything looks good.

Did I do something fundamentally wrong? Will this code work once the DST goes away?  
To be honest I still don't understand why I had to offset my timestamp the the value of `getTimezoneOffset` but also the additional 2 hours in `tzOffset`. I thought it should be able to create a code which always works assuming the timezone on the machine which Node-Red runs on is set up correctly.

Sorry if this has been discussed before, but the other topics I looked were caused by the incorrect timezone setting on the PI, which as I understand is not the case here.

---

<div class="post-metadata">

**Author:** ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)\
**Post date:** [8 September 2024 21:30 UTC](https://discourse.nodered.org/t/understanding-how-to-print-time-in-local-timezone/90797/2 "2024-09-08T21:30:25Z")

</div>

Hi @nygma2004

I really dislike the `Date` object in javascript - I have always seen it being frowned upon by the intermaweb

But, this is where I think the confusion lies (maybe)

`toLocaleString` IS NOT correcting for the timezone, its only representing the date object in a format you specify ('en-GB')

So it's taking the time (which is UTC) and formatting it out, to a format (not timezone) for the language you specify. `YYYY-MM-DD` vs `DD-MM-YYYY` etc etc

if I may,  
You should use a change Node to create a custom formatted string, that does all the heavy lifting for you.

```auto
$moment().tz('Europe/London').format('DD/MM/YYYY HH:mm')

```

 ![Screenshot 2024-09-08 at 22.26.33](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/9/19d6da087171cd54847d817fa63d0a9b360c8876.png)

```auto
[{"id":"74d5b1184da4a405","type":"change","z":"96cd84ddf6640294","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$moment().tz('Europe/London').format('DD/MM/YYYY HH:mm')","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":240,"wires":[["557a0b72e5e9d275"]]},{"id":"eb8aad5bae50df51","type":"inject","z":"96cd84ddf6640294","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":220,"y":240,"wires":[["74d5b1184da4a405"]]},{"id":"557a0b72e5e9d275","type":"debug","z":"96cd84ddf6640294","name":"debug 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":580,"y":240,"wires":[]}]

```

I apologies if I have not locked on to the correct issue (I am not good at how JS manages its `Date` object)

**EDIT**

* * *

I Could be wrong about the behaviour of `toLocaleString` - hence my use of JSONata for this stuff 😅

---

<div class="post-metadata">

**Author:** ![nygma2004](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nygma2004/32/1308_2.png) [@nygma2004](https://discourse.nodered.org/u/nygma2004)\
**Post date:** [8 September 2024 22:23 UTC](https://discourse.nodered.org/t/understanding-how-to-print-time-in-local-timezone/90797/3 "2024-09-08T22:23:48Z")

</div>

Thanks. For the time being, I try to do this without moments, as I sometimes work in environments where I cannot install moments.

---

<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:** [7 December 2024 22:24 UTC](https://discourse.nodered.org/t/understanding-how-to-print-time-in-local-timezone/90797/4 "2024-12-07T22:24:32Z")

</div>

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