# Convert TimeZone to Universal Time Coordinated?

**URL:** <https://discourse.nodered.org/t/convert-timezone-to-universal-time-coordinated/75660>\
**Category:** General\
**Created:** [24 February 2023 01:06 UTC](https://discourse.nodered.org/t/convert-timezone-to-universal-time-coordinated/75660 "2023-02-24T01:06:59Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![lucas3d](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lucas3d/32/16676_2.png) [@lucas3d](https://discourse.nodered.org/u/lucas3d)\
**Post date:** [24 February 2023 01:06 UTC](https://discourse.nodered.org/t/convert-timezone-to-universal-time-coordinated/75660/1 "2023-02-24T01:06:59Z")

</div>

Hi,

I'm looking to convert TimeZone to Universal Time Coordinated (UTC) for one of my projects.  
Initially, the data is an IP address, I'm using the node "node-red-contrib-ip-location-lite" to get a timezone. I'm looking to do this type of conversion:

```auto
* 'Europe/Paris' --------> 'GMT+1'
* 'America/Los_Angeles' -> 'PST'
* 'Pacific/Auckland' ----> 'GMT+13'
* 'America/Toronto ------> 'EST'

```

For now, I'm using many "if" conditions, but I'm sure it exists a better way.  
I have the date and time for managing daylight saving (optional).

Thanks in advance for any subjection.

---

<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:** [24 February 2023 01:17 UTC](https://discourse.nodered.org/t/convert-timezone-to-universal-time-coordinated/75660/2 "2023-02-24T01:17:42Z")

</div>

You can either use the moment node or the MomentJS library built into JSONata.

Alternatively, you can use Node.js's native INTL library in a function node if you either allow modules to be loaded in function nodes or add a require to it in the globals section of your settings.js.

---

<div class="post-metadata">

**Author:** ![lucas3d](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lucas3d/32/16676_2.png) [@lucas3d](https://discourse.nodered.org/u/lucas3d)\
**Post date:** [24 February 2023 02:29 UTC](https://discourse.nodered.org/t/convert-timezone-to-universal-time-coordinated/75660/3 "2023-02-24T02:29:32Z")

</div>

Hi @TotallyInformation ,

I tried to use the moment node but I didn't find a way to my conversion.  
It looks more like for converting date/time than for my conversion. I may miss it.

Regarding your alternative, it's unfortunately outside of my "small" knowledge ☹

Hopping it exists as a simple way to get the TZ name to UTC offset, like this list:

> **[List of tz database time zones | List](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones#List)**
>
> 1.Tz database |
> 2.List of time zone abbreviations |
> 3.Abolition of time zones

Thanks

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [24 February 2023 08:19 UTC](https://discourse.nodered.org/t/convert-timezone-to-universal-time-coordinated/75660/4 "2023-02-24T08:19:03Z")

</div>

~~Have you tried `.utc()`  
e.g `$moment($$.payload).utc().format()` in a change node and selecting JSONata J:~~~  
Scrub that after rereading.

If you provide an example flow of what you are doing we could probably show a simpler way/

---

<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:** [24 February 2023 09:24 UTC](https://discourse.nodered.org/t/convert-timezone-to-universal-time-coordinated/75660/5 "2023-02-24T09:24:49Z")

</div>

> [@lucas3d](#):
>
> I tried to use the moment node but I didn't find a way to my conversion.  
> It looks more like for converting date/time than for my conversion. I may miss it.

Been a while since I wrote or used it but I think you can specify an input tz and an output tz and it will convert automatically? Isn't that what you want?

Ah, re-reading, I see you want to work out what TZ the timestamp is coming from! My bad.

In that case, you need to convert the incoming IP to the 3 letter country code, then you can use that to steer the MomentJS conversion.

Something like this should do it: [geoip-lite/node-geoip: Native NodeJS implementation of MaxMind's GeoIP API -- works in node 0.6.3 and above, ask me about other versions (github.com)](https://github.com/geoip-lite/node-geoip) - however, you will need to add a reference to it in Node-RED and then use a function node.

---

<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:** [24 February 2023 13:42 UTC](https://discourse.nodered.org/t/convert-timezone-to-universal-time-coordinated/75660/6 "2023-02-24T13:42:14Z")

</div>

Do you mean that you have a string that, for example, contains "Europe/Paris", and you want to convert that to the string "GMT+1"?

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [24 February 2023 14:25 UTC](https://discourse.nodered.org/t/convert-timezone-to-universal-time-coordinated/75660/7 "2023-02-24T14:25:07Z")

</div>

> [@lucas3d](#):
>
> I'm using the node "node-red-contrib-ip-location-lite" to get a timezone.

it would be easier if you send us a sample msg from that node to see its structure but luckily there is an example of the node's [page](https://flows.nodered.org/node/node-red-contrib-ip-location-lite)

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

the expression `$moment().tz($$.location.timezone).format("z Z")` in a Change node  
will produce this format `CST -06:00`

test Flow:

```auto
[{"id":"3bbd3677c8b9707f","type":"inject","z":"a479605cda9a7cfd","name":"ip node","props":[{"p":"payload"},{"p":"location.timezone","v":"America/Chicago","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"\"123.123.123.123\"","payloadType":"str","x":250,"y":900,"wires":[["0b7720798a9f4ec7","4c6ca52050b3b727"]]},{"id":"0b7720798a9f4ec7","type":"change","z":"a479605cda9a7cfd","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$moment().tz($$.location.timezone).format(\"z Z\")","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":900,"wires":[["1b814eaabdf51194"]]},{"id":"1b814eaabdf51194","type":"debug","z":"a479605cda9a7cfd","name":"debug 26","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":720,"y":900,"wires":[]},{"id":"4c6ca52050b3b727","type":"debug","z":"a479605cda9a7cfd","name":"debug 27","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":420,"y":820,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![lucas3d](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lucas3d/32/16676_2.png) [@lucas3d](https://discourse.nodered.org/u/lucas3d)\
**Post date:** [25 February 2023 20:29 UTC](https://discourse.nodered.org/t/convert-timezone-to-universal-time-coordinated/75660/8 "2023-02-25T20:29:40Z")

</div>

@UnborN, your code matches perfectly my needs!!!

Sorry, next time I will provide a code example of my issue.  
Thanks a lot, everyone for your subjections!!!

---

<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:** [11 March 2023 20:30 UTC](https://discourse.nodered.org/t/convert-timezone-to-universal-time-coordinated/75660/9 "2023-03-11T20:30:00Z")

</div>

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