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? Show
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 answered Jul 24, 2021 at 8:04
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 answered Mar 26 at 8:11
The Nội dung chính
Nội dung chính
Try itSyntaxReturn valueThe 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
Negative values and positive valuesThe number of minutes returned by Varied results in Daylight Saving Time (DST) regionsIn a region that annually shifts in and out of Daylight Saving Time (DST),
as Note: 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 ExamplesUsing getTimezoneOffset()
getTimezoneOffset() and DSTIn regions that use DST, the return value may change based on the time of the year
getTimezoneOffset() and historical dataDue 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.
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
Browser compatibilityBCD tables only load in the browser See also |