# I just want it to return a string with the current Timezone offsett

**URL:** <https://discourse.nodered.org/t/i-just-want-it-to-return-a-string-with-the-current-timezone-offsett/87123>\
**Category:** General\
**Created:** [10 April 2024 09:54 UTC](https://discourse.nodered.org/t/i-just-want-it-to-return-a-string-with-the-current-timezone-offsett/87123 "2024-04-10T09:54:40Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![IanH](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ianh/32/95002_2.png) [@IanH](https://discourse.nodered.org/u/IanH)\
**Post date:** [10 April 2024 09:54 UTC](https://discourse.nodered.org/t/i-just-want-it-to-return-a-string-with-the-current-timezone-offsett/87123/1 "2024-04-10T09:54:40Z")

</div>

![just Want the Timezone string](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/c/fcc338efee4b5d53b37fad1893e91cf2352e950c.png)

I am just trying to get a string that reports the current Time zone offset aka "+1000" or "+1100" I have already resorted to a function and this is not making sense, I suddenly have offsett of -600, yet a time stamp returned is displaying +1000 which is correct. Thought I could extract it as string using the toLocaleString(...) method but it returns the decimal timestamp converted to a string! And why does debug convert the .getTimezoneOffset() to hex when I expand it. Nothing working as I should and have trouble finding a reference website on the Date() class.

Even posting this is getting weird! So sorry if it looks funny

---

<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:** [10 April 2024 10:03 UTC](https://discourse.nodered.org/t/i-just-want-it-to-return-a-string-with-the-current-timezone-offsett/87123/2 "2024-04-10T10:03:09Z")

</div>

```auto
  const date = new Date()
  const offsetInMinutes = date.getTimezoneOffset()
  const hours = Math.abs(Math.floor(offsetInMinutes / 60))
  const minutes = Math.abs(offsetInMinutes % 60)
  const offsetString = (offsetInMinutes >= 0 ? "-" : "+") + 
                       hours.toString().padStart(2, '0') + 
                       minutes.toString().padStart(2, '0')
  msg.payload = offsetString
  return msg

```

_Untested_

---

<div class="post-metadata">

**Author:** ![ebolisa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ebolisa/32/89203_2.png) [@ebolisa](https://discourse.nodered.org/u/ebolisa)\
**Post date:** [10 April 2024 10:04 UTC](https://discourse.nodered.org/t/i-just-want-it-to-return-a-string-with-the-current-timezone-offsett/87123/3 "2024-04-10T10:04:58Z")

</div>

Your server is suppose to give you your local time.

```auto
if ( !msg.timestamp ) msg.timestamp = Math.round(+ new Date());

var dt = new Date(msg.timestamp);
    msg = {
	'month':	dt.getMonth() + 1,
	'day': dt.getDate(),
	'year': dt.getFullYear(),
	'hours':	dt.getHours(),
	'mins': dt.getMinutes(),
	'msecs':	dt.getMilliseconds()
}

return msg;

```

As an alternative, you can get the offset directly from [here](http://worldtimeapi.org/api/ip).

---

<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:** [10 April 2024 10:17 UTC](https://discourse.nodered.org/t/i-just-want-it-to-return-a-string-with-the-current-timezone-offsett/87123/4 "2024-04-10T10:17:03Z")

</div>

> [@ebolisa](#):
>
> Your server is suppose to give you your local time.

???

Mostly, servers should give you UTC, not local time.

`getTimezoneOffset` returns the number of **minutes** difference between UTC and local time. Not the timezone itself.

This should give you the timezone:

```auto
Intl.DateTimeFormat().resolvedOptions().timeZone

```

---

<div class="post-metadata">

**Author:** ![ebolisa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ebolisa/32/89203_2.png) [@ebolisa](https://discourse.nodered.org/u/ebolisa)\
**Post date:** [10 April 2024 10:27 UTC](https://discourse.nodered.org/t/i-just-want-it-to-return-a-string-with-the-current-timezone-offsett/87123/5 "2024-04-10T10:27:06Z")

</div>

Hum! my home server does give me the local time like any other PCs.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [10 April 2024 10:50 UTC](https://discourse.nodered.org/t/i-just-want-it-to-return-a-string-with-the-current-timezone-offsett/87123/6 "2024-04-10T10:50:56Z")

</div>

> [@ebolisa](#):
>
> my home server does give me the local time like any other PCs.

Show us what you are seeing and how you are fetching it.

---

<div class="post-metadata">

**Author:** ![ebolisa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ebolisa/32/89203_2.png) [@ebolisa](https://discourse.nodered.org/u/ebolisa)\
**Post date:** [10 April 2024 10:56 UTC](https://discourse.nodered.org/t/i-just-want-it-to-return-a-string-with-the-current-timezone-offsett/87123/7 "2024-04-10T10:56:01Z")

</div>

My code shown above gets the local time from my Rpi. Perhaps. I'm missing something here.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [10 April 2024 11:02 UTC](https://discourse.nodered.org/t/i-just-want-it-to-return-a-string-with-the-current-timezone-offsett/87123/8 "2024-04-10T11:02:55Z")

</div>

Do you mean the code  
`msg.time = new Date().getTime()`

For me that returns the number of milliseconds since The Beginning (which is UTC).

---

<div class="post-metadata">

**Author:** ![ebolisa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ebolisa/32/89203_2.png) [@ebolisa](https://discourse.nodered.org/u/ebolisa)\
**Post date:** [10 April 2024 11:06 UTC](https://discourse.nodered.org/t/i-just-want-it-to-return-a-string-with-the-current-timezone-offsett/87123/9 "2024-04-10T11:06:41Z")

</div>

No. As shown in my msg: `if ( !msg.timestamp ) msg.timestamp = Math.round(+ new Date());`

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [10 April 2024 11:12 UTC](https://discourse.nodered.org/t/i-just-want-it-to-return-a-string-with-the-current-timezone-offsett/87123/10 "2024-04-10T11:12:31Z")

</div>

> [@ebolisa](#):
>
> Math.round(+ new Date());

What does this give you in a function node?

```auto
msg.payload = Math.round(+ new Date());
return msg;

```

The reason you are getting local time from such functions as Date.getHours() is because the definition of those functions is specifically to give you the hours in local time. It converts the milliseconds ticks (UTC) into the local time for you.

---

<div class="post-metadata">

**Author:** ![ebolisa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ebolisa/32/89203_2.png) [@ebolisa](https://discourse.nodered.org/u/ebolisa)\
**Post date:** [10 April 2024 11:17 UTC](https://discourse.nodered.org/t/i-just-want-it-to-return-a-string-with-the-current-timezone-offsett/87123/11 "2024-04-10T11:17:04Z")

</div>

Epoch (UTC in seconds) time. Anyway, I'm not the one who's requesting help on this thread 😉

---

<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:** [10 April 2024 13:50 UTC](https://discourse.nodered.org/t/i-just-want-it-to-return-a-string-with-the-current-timezone-offsett/87123/12 "2024-04-10T13:50:43Z")

</div>

> [@ebolisa](#):
>
> Hum! my home server does give me the local time like any other PCs.

As I often say to people, you are much better off working in UTC except for where someone inputs a time or needs to see a time. It avoids soooo many processing issues.

---

<div class="post-metadata">

**Author:** ![ebolisa](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ebolisa/32/89203_2.png) [@ebolisa](https://discourse.nodered.org/u/ebolisa)\
**Post date:** [10 April 2024 15:06 UTC](https://discourse.nodered.org/t/i-just-want-it-to-return-a-string-with-the-current-timezone-offsett/87123/13 "2024-04-10T15:06:36Z")

</div>

I agree! In facts, all my time related projects use UTC.

---

<div class="post-metadata">

**Author:** ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)\
**Post date:** [10 April 2024 16:38 UTC](https://discourse.nodered.org/t/i-just-want-it-to-return-a-string-with-the-current-timezone-offsett/87123/14 "2024-04-10T16:38:11Z")

</div>

To get the '`+0100`' part of a dateTime string the function below will work.

If `date.toString()` does not return something like `Wed Apr 10 2024 17:33:46 GMT+0100 (British Summer Time)` you will have to adjust `GMT` to whatever comes before the time zone offset.

```auto
const date = new Date()
const dateTimeStr = date.toString()
const timeZoneIndex = dateTimeStr.indexOf('GMT')
const timeZoneOffset = dateTimeStr.slice(timeZoneIndex + 3, timeZoneIndex + 8)

return timeZoneOffset

```

---

<div class="post-metadata">

**Author:** ![IanH](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ianh/32/95002_2.png) [@IanH](https://discourse.nodered.org/u/IanH)\
**Post date:** [10 April 2024 22:41 UTC](https://discourse.nodered.org/t/i-just-want-it-to-return-a-string-with-the-current-timezone-offsett/87123/15 "2024-04-10T22:41:51Z")

</div>

I agree, NR and Mosquito all run a Raspi on my local network, the Raspi and router which is a NTP server displays correct time. so not sure why I got -600 instead of +1000. THanks anyway

---

<div class="post-metadata">

**Author:** ![IanH](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ianh/32/95002_2.png) [@IanH](https://discourse.nodered.org/u/IanH)\
**Post date:** [11 April 2024 05:11 UTC](https://discourse.nodered.org/t/i-just-want-it-to-return-a-string-with-the-current-timezone-offsett/87123/16 "2024-04-11T05:11:57Z")

</div>

Thanks all, it appears I was expecting the .getTimezoneOffset() to return a TZ string aka "+1000" but appears to return it as -ve minutes wrt Zulu (-600). would have sussed that if I had found usable documentation ([developer.mozilla.org](http://developer.mozilla.org) was useless!) last night but was late. I have now found it in W3schools, not quite complete but it has most.

I first tried Steve McL code and it works. I will have to wait until October to verify it does shift to Summer time but expect it will.

THanks all

---

<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:** [11 April 2024 08:00 UTC](https://discourse.nodered.org/t/i-just-want-it-to-return-a-string-with-the-current-timezone-offsett/87123/17 "2024-04-11T08:00:46Z")

</div>

> [@IanH](#):
>
> it appears I was expecting the .getTimezoneOffset() to return a TZ string aka "+1000" but appears to return it as -ve minutes wrt Zulu (-600). would have sussed that if I had found usable documentation ([developer.mozilla.org](http://developer.mozilla.org) was useless!)

This is exactly what I said earlier in the thread. I got the info from MDN so maybe not so useless. I also showed how to get the actual timezone (not the offset). The offset could, of course, be derived from the number of minutes from UTC.

---

<div class="post-metadata">

**Author:** ![maxwellhadley](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/maxwellhadley/32/7827_2.png) [@maxwellhadley](https://discourse.nodered.org/u/maxwellhadley)\
**Post date:** [11 April 2024 15:50 UTC](https://discourse.nodered.org/t/i-just-want-it-to-return-a-string-with-the-current-timezone-offsett/87123/18 "2024-04-11T15:50:50Z")

</div>

Reminds me of the time I was given a dataset to analyse, covering ten hours or so but with an unknown time zone setting. Nobody could help, but I did find a value within the dataset that was probably following (approximately) the ambient temperature. I did know where the data was measured, so I found the closest airfield and downloaded the aftercast for the approximate time span. A bit of judicious correlation-by-eye and I had the time zone offset. But there are easier ways…

---

<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:** [25 April 2024 15:51 UTC](https://discourse.nodered.org/t/i-just-want-it-to-return-a-string-with-the-current-timezone-offsett/87123/19 "2024-04-25T15:51:24Z")

</div>

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