How to get Australia Sydney timezone offset in javascript nodejs

0 votes
Austalia/Sydney timezone offset (UTC to AEST difference) is +10:00 (from April first Sunday) and +11:00 (from April first Sunday) (from October first Sunday).

There is a 600 minute or 660 minute difference.

In javascript/nodejs/momentjs, how can I retrieve the time difference? Regardless of server time?
Jun 14, 2022 in Node-js by Vaani
• 7,070 points
840 views

1 answer to this question.

0 votes

I haven't done any study to find out whether there is a "better" method to accomplish this, but then why would I?

This function was written in a matter of minutes using Intl.DateTimeFormat

Modern browsers and nodejs are supported.

function getTZoffset(timeZone, d, m, y) {
  const date = Date.UTC(y, m - 1, d, 0, 0, 0);
  const tgt = Intl.DateTimeFormat('en', { timeZone, dateStyle: "short", timeStyle: 'short', hour12: false }).format(date).split(',');
  const utc = Intl.DateTimeFormat('en', { timeZone: 'UTC', dateStyle: "short" }).format(date).split(',');
  const tgtTimes = tgt[1].split(':').map(Number);
  const tgtTime = (tgtTimes[0] % 24) * 60 + tgtTimes[1]
  const utcTime = tgt[0] === utc[0] ? 0 : 24 * 60;
  return tgtTime - utcTime;
}

for (let m = 1; m <= 12; m++) {
  console.log(`Sydney/Australia 1/${m}/2020: ${getTZoffset('Australia/Sydney', 1, m, 2020)}`);
}
for (let m = 1; m <= 12; m++) {
  console.log(`America/New_York 1/${m}/2020: ${getTZoffset('America/New_York', 1, m, 2020)}`);
}
answered Jun 14, 2022 by Neha
• 9,020 points

Related Questions In Node-js

0 votes
1 answer

How to get the _id of inserted document in Mongo database in NodeJS?

Hello @kartik, A shorter way than using second ...READ MORE

answered Sep 7, 2020 in Node-js by Niroj
• 82,840 points
13,772 views
0 votes
1 answer

How to get path from the request in nodejs?

Hello @kartik, Try this out: var http = require('http'); var ...READ MORE

answered Oct 14, 2020 in Node-js by Niroj
• 82,840 points
4,526 views
0 votes
1 answer

How to get GET (query string) variables in Express.js on Node.js?

Hello @kartik, Since you've mentioned Express.js in your ...READ MORE

answered Jul 8, 2020 in Node-js by Niroj
• 82,840 points
3,260 views
0 votes
1 answer

How to get access to webpack-dev-server from devices in local network?

Hello @kartik, You can set your ip address ...READ MORE

answered Jul 17, 2020 in Node-js by Niroj
• 82,840 points
6,106 views
0 votes
1 answer
0 votes
0 answers

No US HAWAII time zone in Momentjs or nodejs

How can I use Momentjs to adjust ...READ MORE

Jun 23, 2022 in Node-js by Vaani
• 7,070 points
558 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,927 views
0 votes
1 answer

How to schedule a google meet and get the meet link in NodeJs?

To create a Google Meet, you'll need ...READ MORE

answered May 27, 2022 in Node-js by Neha
• 9,020 points
3,878 views
0 votes
1 answer

How to use pino-transport in nodejs for logs?

Ensure that you have a recent version ...READ MORE

answered Jun 9, 2022 in Node-js by Neha
• 9,020 points
5,229 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP