浏览器获取本地时间与格林威治时的偏差

可以使用 Date 对象的 getTimezoneOffset()来获取分钟差

// Get current timezone offset for host device
var x = new Date();
var currentTimeZoneOffsetInHours = x.getTimezoneOffset() / 60;
// 1

// Get timezone offset for International Labour Day (May 1) in 2016
// Be careful, the Date() constructor uses 0-indexed month so May is
// represented with 4 (and not 5)
var labourDay = new Date(2016, 4, 1)
var labourDayOffset = labourDay.getTimezoneOffset() / 60;

除以60分钟,返回的是小时,东八区,就是-8

Leave a Comment

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据