# Help with javascript moment-timezone

**URL:** <https://discourse.nodered.org/t/help-with-javascript-moment-timezone/94638>\
**Category:** General\
**Created:** [15 January 2025 08:58 UTC](https://discourse.nodered.org/t/help-with-javascript-moment-timezone/94638 "2025-01-15T08:58:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ThingsTinkerer](https://avatars.discourse-cdn.com/v4/letter/t/91b2a8/32.png) [@ThingsTinkerer](https://discourse.nodered.org/u/ThingsTinkerer)\
**Post date:** [15 January 2025 08:58 UTC](https://discourse.nodered.org/t/help-with-javascript-moment-timezone/94638/1 "2025-01-15T08:58:08Z")

</div>

Desperate for help here, sorry this pure javascript (with moment-timezone library), not directly tied to node-red, but need this to work in node red regardless.

I got 2 input strings:

1. date (string on format "yyyy-MM-dd hh:mm")
2. tz (timezone string, for example "Europe/Oslo")

Seconds doesn't matter here. I need this converted to unix time epoc number. It looked straight forwad, but no matter what I do with tz, it produce same output!

```auto
import moment from "moment-timezone";

const localTime = "2025-01-15 00:00";
const timezone1 = "Europe/Oslo";
const timezone2 = "Australia/Melbourne";

function localTimeToUnixTimestamp(localDateTime, timezone) {
  const m = moment.tz(localDateTime, 'YYYY-MM-DD HH:mm', timezone);
  return m.unix();
}

// Example usage:
const a = localTimeToUnixTimestamp(localTime, timezone1); // 1736937000  
const b = localTimeToUnixTimestamp(localTime, timezone2); // 1736937000

console.log(a == b); // true !?!

```

If I'm lucky someone has some experience with this and can help me trapped in timezone nigthmare haha.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [15 January 2025 10:36 UTC](https://discourse.nodered.org/t/help-with-javascript-moment-timezone/94638/2 "2025-01-15T10:36:08Z")

</div>

I am way out of my depth here.

So the code you posted:

```auto
const a = localTimeToUnixTimestamp(localTime, timezone1); // 1736937000  
const b = localTimeToUnixTimestamp(localTime, timezone2); // 1736937000

console.log(a == b); // true !?!

```

I am confused with the last line.

And most of the code. But from an outside perspective:

You define two zones so when you send the time to the code it _translates_ it from the _local_ time to Unix time.

So what is this last line: `console.log(a == b);`  
(I posted this as I saw someone else replying and hoped they may help you better than I can.)

You _call_ your routine and get back a return (which from the routine's point of view is `m`.  
But is either `a` or `b` in the last lines.

So I am at a huge loss why you are saying `(a == b)`

---

<div class="post-metadata">

**Author:** ![ThingsTinkerer](https://avatars.discourse-cdn.com/v4/letter/t/91b2a8/32.png) [@ThingsTinkerer](https://discourse.nodered.org/u/ThingsTinkerer)\
**Post date:** [15 January 2025 11:03 UTC](https://discourse.nodered.org/t/help-with-javascript-moment-timezone/94638/3 "2025-01-15T11:03:11Z")

</div>

Sorry it was something else completely. The reason I had a == b was I tried to set different timezones, which should give different result. But they were always identical (indicating timezone was not effectively applied).

I was playing around with code in vscode using a repl js extension to get live feedback. Turns out timezones wasn't loaded correctly there 😲Took a few hours before I figured out haha.

So when running the js file from command line, or in node red, it should take timezone into account.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [15 January 2025 11:09 UTC](https://discourse.nodered.org/t/help-with-javascript-moment-timezone/94638/4 "2025-01-15T11:09:20Z")

</div>

Glad I helped - or I hope I did.

All the best.

---

<div class="post-metadata">

**Author:** ![ThingsTinkerer](https://avatars.discourse-cdn.com/v4/letter/t/91b2a8/32.png) [@ThingsTinkerer](https://discourse.nodered.org/u/ThingsTinkerer)\
**Post date:** [15 January 2025 13:21 UTC](https://discourse.nodered.org/t/help-with-javascript-moment-timezone/94638/5 "2025-01-15T13:21:07Z")

</div>

Yes thanks a lot, always helpful. Strange situation when playing around in vs code. When running from command line (`node .\src\test.js`), it works with normal `import moment from "moment-timezone";`. But when running JS REPL plugin in vscode, that won't load the timezone data properly, so it just ignores it silently. So you still get timestamps or whatever without taking timezone into account... Turns out a different import worked for JS REPL (but breaks command line haha): `import moment from "moment-timezone/builds/moment-timezone-with-data";`

---

<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:** [29 January 2025 13:21 UTC](https://discourse.nodered.org/t/help-with-javascript-moment-timezone/94638/6 "2025-01-29T13:21:42Z")

</div>

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