Hướng dẫn nodejs get current timezone

I've been having this issue for months and I've finally made some headway. I'm writing an app the sends me a message at specific times, 9 am and 9 pm eastern time. When I ran it locally it worked perfectly but when I deploy it, I get nothing. I was messing around and then I saw this Heroku Logs. My guess is that my app is located on a server that is in a different time zone and when this code below runs. The conditions are never met and nothing gets sent. My question now is, is there a way I can get the current time of and compare regardless of what time zone the server is located?

const sendMessage = require('./sms-api.js');

const send = () =>{

    setInterval(()=>{

        var x = new Date().toLocaleTimeString();
        console.log(x);
    
       if(x === '11:00:10 AM')
       {
           console.log('match');
           return sendMessage('6178032176', 'Good Morning');
       }
       else if(x === '9:50:20 PM')
       {
           console.log('match');
           sendMessage('6178032176', 'Good Evening');
       }
    },1000)
}

send();

asked Jul 24, 2021 at 7:54

When working with different timezones, it is better to work in UTC and then offset it according to required timezone.

Get the time in UTC and then offset it according to required timezone.

You can also use dedicated libraries like moment-timezone.
https://momentjs.com/timezone/docs/

answered Jul 24, 2021 at 8:04

Hướng dẫn nodejs get current timezone

Suyash GaurSuyash Gaur

1,9852 gold badges8 silver badges18 bronze badges

0

Like Suyash said above, your best option is to work entirely in UTC, and only convert when displaying times to users. Rather than dealing with offsets, you can append your dates and times with a 'Z' to indicate they are universal.

The best way I've found to do that is with moment.js and moment-timezone.js. Here is an example of an implementation that will allow you to convert times and dates: https://github.com/aidanjrauscher/browser-timezone-conversions. These libraries also make it very convenient to convert any date or time related user input back from their local time zone to UTC.

answered Jul 24, 2021 at 16:25

thank you for your help. I ended up figuring it out. I used this instead const time = new Date().toLocaleTimeString('en-US', { timeZone: 'America/New_York' });.

answered Mar 26 at 8:11

The getTimezoneOffset() method returns the difference, in minutes, between a date as evaluated in the UTC time zone, and the same date as evaluated in the local time zone.

Nội dung chính

  • Return value
  • Description
  • Negative values and positive values
  • Varied results in Daylight Saving Time (DST) regions
  • Using getTimezoneOffset()
  • getTimezoneOffset() and DST
  • getTimezoneOffset() and historical data
  • Specifications
  • Browser compatibility

Nội dung chính

  • Return value
  • Description
  • Negative values and positive values
  • Varied results in Daylight Saving Time (DST) regions
  • Using getTimezoneOffset()
  • getTimezoneOffset() and DST
  • getTimezoneOffset() and historical data
  • Specifications
  • Browser compatibility

Try it

Syntax

Return value

The difference, in minutes, between the date as evaluated in the UTC time zone and as evaluated in the local time zone. The actual local time algorithm is implementation-defined, and the return value is allowed to be zero in runtimes without appropriate data.

Description

date.getTimezoneOffset() returns the difference, in minutes, between date as evaluated in the UTC time zone and as evaluated in the local time zone — that is, the time zone of the host system in which the browser is being used (if the code is run from the Web in a browser), or otherwise the host system of whatever JavaScript runtime (for example, a Node.js environment) the code is executed in.

Negative values and positive values

The number of minutes returned by getTimezoneOffset() is positive if the local time zone is behind UTC, and negative if the local time zone is ahead of UTC. For example, for UTC+10, -600 will be returned.

Varied results in Daylight Saving Time (DST) regions

In a region that annually shifts in and out of Daylight Saving Time (DST), as date varies, the number of minutes returned by calling getTimezoneOffset() can be non-uniform.

Note: getTimezoneOffset()'s behavior will never differ based on the time when the code is run — its behavior is always consistent when running in the same region. Only the value of date affects the result.

In most implementations, the IANA time zone database (tzdata) is used to precisely determine the offset of the local timezone at the moment of the date. However, if such information is unavailable, an implementation may return zero.

Examples

Using getTimezoneOffset()

// Create a Date instance for the current time
const currentLocalDate = new Date();
// Create a Date instance for 03:24 GMT-0200 on May 1st in 2016
const laborDay2016at0324GMTminus2 = new Date('2016-05-01T03:24:00Z-02:00');
currentLocalDate.getTimezoneOffset() === laborDay2016at0324GMTminus2.getTimezoneOffset();
// true, always, in any timezone that doesn't annually shift in and out of DST
// false, sometimes, in any timezone that annually shifts in and out of DST

getTimezoneOffset() and DST

In regions that use DST, the return value may change based on the time of the year date is in. Below is the output in a runtime in New York, where the timezone is UTC-05:00.

const nyOffsetSummer = new Date('2022-02-01').getTimezoneOffset(); // 300
const nyOffsetWinter = new Date('2022-08-01').getTimezoneOffset(); // 240

getTimezoneOffset() and historical data

Due to historical reasons, the timezone a region is in can be constantly changing, even disregarding DST. For example, below is the output in a runtime in Shanghai, where the timezone is UTC+08:00.

const shModernOffset = new Date('2022-01-27').getTimezoneOffset(); // -480
const shHistoricalOffset = new Date('1943-01-27').getTimezoneOffset(); // -540

This is because during the Sino-Japanese War when Shanghai was under Japanese control, the timezone was changed to UTC+09:00 to align with Japan's (in effect, it was a "year-round DST"), and this was recorded in the IANA database.

Specifications

Specification
ECMAScript Language Specification
# sec-date.prototype.gettimezoneoffset

Browser compatibility

BCD tables only load in the browser

See also