Javascript Date getTimezoneOffset()

Introduction

Javascript Date getTimezoneOffset() returns the number of minutes that the local time zone is offset from UTC.

The offset is positive if the local timezone is behind UTC and negative if it is ahead.

Current Locale UTC-8 UTC UTC+3
Return Value 480 0 -180

For example, Eastern Standard Time returns 300.

// if you are in timezone UTC+3, outputs -180
console.log( new Date().getTimezoneOffset() );
// Get current timezone offset for host device
let x = new Date();
let currentTimeZoneOffsetInHours = x.getTimezoneOffset() / 60;
console.log(currentTimeZoneOffsetInHours);


// Get timezone offset for May 1, 2020
// the Date() constructor uses 0-indexed month so May is represented with 4 (and not 5)
let labourDay = new Date(2020, 4, 1)
let labourDayOffset = labourDay.getTimezoneOffset() / 60;
console.log(labourDayOffset);/*ww w .j a v a 2 s  . c  om*/

This value changes when an area goes into Daylight Saving Time.




PreviousNext

Related