# Using date-fns-tz additional module

**URL:** <https://discourse.nodered.org/t/using-date-fns-tz-additional-module/90435>\
**Category:** General\
**Tags:** function-node\
**Created:** [26 August 2024 00:45 UTC](https://discourse.nodered.org/t/using-date-fns-tz-additional-module/90435 "2024-08-26T00:45:04Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![dangruhn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dangruhn/32/91097_2.png) [@dangruhn](https://discourse.nodered.org/u/dangruhn)\
**Post date:** [26 August 2024 00:45 UTC](https://discourse.nodered.org/t/using-date-fns-tz-additional-module/90435/1 "2024-08-26T00:45:04Z")

</div>

Hi all, first time poster here.  
Node-RED version: v3.1.9  
Node.js version: v18.20.4  
Linux 5.15.0-119-generic x64 LE  
I'm trying to get my feet wet with node-red as home automation software and thus far am enjoying the possibilities.

I'm trying to use the date-fns-tz module so that times that are entered for when a particular event happens can by entered in local time (America/New\_York) but internally everything happens in UTC (which my Linux system runs as).

I have installed date-fns and date-fns-tz using npm from the .node-red directory and have set them up in settings.js like this:

` functionExternalModules: true,`

and

```auto
    functionGlobalContext: {
        os:require('os'),
        tplinkApiModule: require('tplink-smarthome-api'),
        dateFns: require('date-fns'),
        dateFnsTz: require('date-fns-tz')
    },

```

but from there things are a bit fuzzy for me.  
I have tried a function node with

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/4/240d90fa912dd7a6662a8815e69ea55bafc60c4a.png)  
and then have code like:

```auto
const date = new Date();
const timeZone = env.get("TZ");
const zonedDate = utcToZonedTime(date, timeZone);
const offset = zonedDate.getTimezoneOffset();
node.warn(`${date} - ${zonedDate} - ${offset}`);

```

but I get this error:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/4/04c74f71c402d3ab39df355da5540ddeb9050385.png)

I feel like I'm close but can't seem to get the right combination of directives.

Perhaps there is another way to do what I'm trying to do, but I'd like to be able to use additional modules in any case.

Any help greatly appreciated.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [26 August 2024 09:43 UTC](https://discourse.nodered.org/t/using-date-fns-tz-additional-module/90435/2 "2024-08-26T09:43:42Z")

</div>

Here is one way you could do it using a `function` node

 ![Screenshot 2024-08-26 at 5.41.47 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/8/88ce78ae619f13548316fbd753e10096c58bd192.png)

```auto
[{"id":"95c1661d8a920cb7","type":"inject","z":"cc74683187f0b740","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":960,"y":140,"wires":[["589790794cc1c8ab","514e4da892286197"]]},{"id":"589790794cc1c8ab","type":"debug","z":"cc74683187f0b740","name":"Raw time value","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1180,"y":120,"wires":[]},{"id":"0a96e628c5d41ec1","type":"debug","z":"cc74683187f0b740","name":"Converted date/time","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1200,"y":240,"wires":[]},{"id":"514e4da892286197","type":"function","z":"cc74683187f0b740","name":"convert time to local date/time","func":"let now_time = msg.payload\nlet d = new Date(now_time);\nmsg.payload = d.toLocaleString(\"en-US\")\n\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1230,"y":180,"wires":[["0a96e628c5d41ec1"]]}]

```

---

<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:** [26 August 2024 09:55 UTC](https://discourse.nodered.org/t/using-date-fns-tz-additional-module/90435/3 "2024-08-26T09:55:09Z")

</div>

From the date-fns-tz docs

> Usage with Node.js
> 
> Node.js supports the `Intl` API and ships with full ICU data included in the binary since v13, so this library works by default.

> **[Intl.DateTimeFormat - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat)**
>
> The Intl.DateTimeFormat object enables language-sensitive date and time formatting.

> **[Intl - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#static_properties)**
>
> The Intl namespace object contains several constructors as well as functionality common to the internationalization constructors and other language sensitive functions. Collectively, they comprise the ECMAScript Internationalization API, which...

You should also be able to use moments in a change node. or a function node [Search results for 'format date with moment' - Node-RED Forum](https://discourse.nodered.org/search?q=format%20date%20with%20moment)

Or there is a moment node [node-red-contrib-moment (node) - Node-RED](https://flows.nodered.org/node/node-red-contrib-moment)

---

<div class="post-metadata">

**Author:** ![dangruhn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dangruhn/32/91097_2.png) [@dangruhn](https://discourse.nodered.org/u/dangruhn)\
**Post date:** [26 August 2024 13:53 UTC](https://discourse.nodered.org/t/using-date-fns-tz-additional-module/90435/4 "2024-08-26T13:53:18Z")

</div>

I see that I've not clearly described what I am trying to do.  
I **have a local time** value in that I wish to get the timestamp of. It is not that I need to **display** it in local time, I need to convert it to UTC for a proper timestamp.

---

<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:** [26 August 2024 14:06 UTC](https://discourse.nodered.org/t/using-date-fns-tz-additional-module/90435/5 "2024-08-26T14:06:11Z")

</div>

> [@dangruhn](#):
>
> I see that I've not clearly described what I am trying to do.  
> I **have a local time** value in that I wish to get the timestamp of.

You still have not clearly described your issue, how about some examples of what you have and what you want.

---

<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:** [26 August 2024 14:08 UTC](https://discourse.nodered.org/t/using-date-fns-tz-additional-module/90435/6 "2024-08-26T14:08:49Z")

</div>

Built in moment can do this in a change node.

Alternatively in a function node with

```auto

const d = new Date(msg.payload)
msg.payload = {}

// Get UTC ms since epoch
msg.payload.ts= d.valueOf()

// Get iso time
msg.payload.iso = d.toISOString()

return msg

```

_(Assumes the time to convert is in msg.payload)_

Ref: [Date.prototype.toISOString() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString)

> [@dangruhn](#):
>
> I **have a local time** value in that I wish to get the timestamp of

I assume you mean an iso string because I associate `timestamp` with the numeric Epoch in JavaScript.

---

<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:** [26 August 2024 14:26 UTC](https://discourse.nodered.org/t/using-date-fns-tz-additional-module/90435/7 "2024-08-26T14:26:02Z")

</div>

> [@dangruhn](#):
>
> times that are entered for when a particular event happens can by entered in local time (America/New\_York)

Exactly what form do you have the date and time in, and exactly what form do you want it in?

---

<div class="post-metadata">

**Author:** ![dangruhn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dangruhn/32/91097_2.png) [@dangruhn](https://discourse.nodered.org/u/dangruhn)\
**Post date:** [26 August 2024 17:00 UTC](https://discourse.nodered.org/t/using-date-fns-tz-additional-module/90435/8 "2024-08-26T17:00:34Z")

</div>

Thanks for helping me.

The Linux system runs at UTC, but the node-red TZ environmental variable is set to America/New\_York, which I want entered times to be based on (it could change).

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/1/a17e9d4b9cdc0d54dfb78925dfd5226236eaf5bc.png)

I have the entered time as "19:00" or "06:30" from a configuration table and I would like to get an epoch timestamp of those times with the currently set TZ timezone.

I am not using date on the entries because these times happen daily.

---

<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:** [26 August 2024 17:08 UTC](https://discourse.nodered.org/t/using-date-fns-tz-additional-module/90435/9 "2024-08-26T17:08:05Z")

</div>

Do you want the UTC time for that time on the current date?

What exactly do you mean by epoch timestamp? A javascript ticks since epoch (milliseconds)? A javascript Date object? A Unix timestamp (seconds)?

---

<div class="post-metadata">

**Author:** ![dangruhn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dangruhn/32/91097_2.png) [@dangruhn](https://discourse.nodered.org/u/dangruhn)\
**Post date:** [26 August 2024 18:13 UTC](https://discourse.nodered.org/t/using-date-fns-tz-additional-module/90435/10 "2024-08-26T18:13:10Z")

</div>

Yes, I want the UTC time for that time on the current date.

By epoch timestamp, I mean the javascript ticks since epoch (milliseconds).

My program is trying to determine where the current time fits in a list of possible time slots in the current day.

---

<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:** [26 August 2024 18:21 UTC](https://discourse.nodered.org/t/using-date-fns-tz-additional-module/90435/11 "2024-08-26T18:21:29Z")

</div>

> [@dangruhn](#):
>
> I mean the javascript ticks since epoch (milliseconds)

[valueOf](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/valueOf) as offered in my post: [Using date-fns-tz additional module - #6 by Steve-Mcl](https://discourse.nodered.org/t/using-date-fns-tz-additional-module/90435/6)

---

<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:** [26 August 2024 19:00 UTC](https://discourse.nodered.org/t/using-date-fns-tz-additional-module/90435/12 "2024-08-26T19:00:10Z")

</div>

As said moment in a change node using JSONata can do this

```auto
[{"id":"27f922a45cfb6ed9","type":"inject","z":"0df28c03098e1a7f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"06:30","payloadType":"str","x":170,"y":520,"wires":[["3311096d42d8d652"]]},{"id":"3311096d42d8d652","type":"change","z":"0df28c03098e1a7f","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$number($moment($$.payload, \"HH:mm\").format(\"x\"))","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":520,"wires":[["27e213a1e3bfa8a4"]]},{"id":"27e213a1e3bfa8a4","type":"debug","z":"0df28c03098e1a7f","name":"debug 2567","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":550,"y":520,"wires":[]}]

```

```auto
$number($moment($$.payload, "HH:mm").format("x"))

```

Where msg.payload holds the string `06:30`  
This will output

```auto
1724650200000

```

or this

```auto
$moment($$.payload, "HH:mm").utc().format()

```

will output

```auto
"2024-08-26T06:30:00Z"

```

or this

```auto
$moment($$.payload, "HH:mm").format()

```

will output

```auto
"2024-08-26T06:30:00+01:00"

```

---

<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:** [24 November 2024 19:00 UTC](https://discourse.nodered.org/t/using-date-fns-tz-additional-module/90435/13 "2024-11-24T19:00:31Z")

</div>

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