function dateBJtoLocal(time) {
// 当前时区与标准地区的差值(分钟)
const offset = new Date().getTimezoneOffset();
// 北京时间戳
const bjDate = new Date(time).getTime();
// 当地时间戳
const ddDate = (-offset / 60 - 8) * 60 * 60 * 1000 + bjDate;
const date = new Date(ddDate);
return date;
}
大体思路:
1,获取某个北京时间的时间戳
2.计算当地时区和 0 时区的差值(分钟)
3.使用北京时间戳加上差值的时间戳及为北京时间对应的当地时间
PS:可以修改电脑的 日期与时间/时区,然后打开浏览器验证