Timezones...server & browser

Hello, I am reasonably proficient with Node Red, but at the moment, I don't have a solution to my problem.
I use Dashboard to display a graph of data over time. It works great. If I'm on the West Coast, the "last three hours" uses the correct times as x axis. I changed Windows to say I'm on the East Coast, and the x axis times update perfectly without a page refresh. Fabulous!

On the same page, I pull longer-term data from InfluxDB. I run the results thru a function node (called "prettify graph") where I massage the database's results into human friendly results. Part of that is, naturally, rendering the (user's) time.
Sharing my code will help the context of my question:

now = new Date();
const user_timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
now.toLocaleString('en-US', { timeZone: user_timezone })
node.log("user_timezone is "+user_timezone);
node.log("TZ offset is "+now.getTimezoneOffset(user_timezone) );
for (var x in rtn.labels) {
    var this_time = Date.parse(rtn.labels[x]);
    rtn.labels[x] = new Intl.DateTimeFormat('en-US', { date:false, hour:'numeric',minute:'numeric', hour12: true, timeZone:user_timezone }).format(this_time);

So, const user_timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
is great for finding what the Browser sees is the timezone. BUT, I'm running node red on a linux host unchanged as UTC, and I'm doing the sensible thing of keeping it that way, because I may have viewers from any timezone. When I look at the node log on the server, alas and of course, I see
[function:Prettify graph] user_timezone is UTC
[function:Prettify graph] TZ offset is 0

The question: How do I get the browser's results of const user_timezone = Intl.DateTimeFormat().resolvedOptions().timeZone; into the context of the server's node/javascript/vm execution environment? I bet it's "simple", but I can't think of it nor find it. (I cited that Dashboard's Line Chart node behaves correctly, but I think that's a because it does the rendering on the browser, not the server.)

Thanks in advance for pondering my question!


I honestly can't say I've had EXEACTLY your issue, but I have had to do some time-zone 'manipulation' to solve display issues.

The easiest way for me was to install the 'moment' node (node-red-contrib-moment) and explore it. Forcing all TZ to UTC was helpful - DB, UI etc.

I hope this helps - perhaps in a small way.



Thanks @paulkeates I've used moment and it can be very helpful. Alas, I fear moment wouldn't have the (client-located) information that's needed, either.
What I need (I think) is for the server to know a bit of information (the timezone) from the browser, but without the user having to do anything to send that information up. HTTP headers came to mind, but I see they do not contain the browser's timezone.
I just want a single simple line of javascript to run in the browser and to report the value up to node. (sort of the approach mentioned here, in SO's 13th question: html - Determine a user's timezone - Stack Overflow)

Thinking aloud...
I've used the OnConnect node to kick off nodes that can even force information down to the browser. Separately, I could probably have a bit of node.js code that runs a bit of client initialization when that hits the browser. Then the trick would be to actively fetch or await a push of that (timezone) information.

Pondering continues...

You can use the ui-template to return browser Date() properties
Take a look here

Awesome, Thanks! I'll mark this as the solution once I get to try it out.

Oh phew, ui-template not ui-builder. I want to get familiar with ui-builder but not to solve this.
I see the nice little importable snippet over on the other page, and was able to import and try it out and found that it'll solve my problem. Thanks

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