Thanks to visit codestin.com
Credit goes to coreui.io

How to get the timezone offset in JavaScript

Getting timezone offset is crucial for handling date calculations across different timezones and converting between local and UTC times in JavaScript applications. As the creator of CoreUI, a widely used open-source UI library, I’ve handled timezone calculations in countless global applications over 25 years of development. From my expertise, the most reliable approach is using the getTimezoneOffset() method, which returns the difference in minutes between UTC and local time. This provides the foundation for accurate timezone-aware date operations.

Use getTimezoneOffset() to get the timezone difference in minutes from UTC.

const offset = new Date().getTimezoneOffset()
const hours = Math.abs(offset / 60)
const sign = offset > 0 ? '-' : '+'

Here getTimezoneOffset() returns the timezone offset in minutes, where positive values indicate timezones behind UTC and negative values indicate timezones ahead. The method calculates hours by dividing by 60, and determines the sign where positive offset means the local timezone is behind UTC (like PST is UTC-8).

Best Practice Note:

This is the same timezone handling approach we use in CoreUI components for global date operations. Remember that the offset can change due to daylight saving time, so always get the offset for the specific date you’re working with rather than caching it.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author

Subscribe to our newsletter
Get early information about new products, product updates and blog posts.
How to Redirect to a New URL Using JavaScript Redirect Techniques
How to Redirect to a New URL Using JavaScript Redirect Techniques

How to fix “SyntaxError: Cannot use import statement outside a module”?
How to fix “SyntaxError: Cannot use import statement outside a module”?

How to sleep in Javascript
How to sleep in Javascript

What is the Difference Between Null and Undefined in JavaScript
What is the Difference Between Null and Undefined in JavaScript

Answers by CoreUI Core Team