Internet Explorer 11计算的时区错误 [英] Internet Explorer 11 computed timezone bug

查看:74
本文介绍了Internet Explorer 11计算的时区错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道问题的根源,并且用了半天时间在Google上搜索,但仍然找不到任何好的解决方法或解决方案.

I am aware of the origin of the problem and googled like half a day, but still couldn't find any good workarounds or solutions for this.

我们的Angular 7+应用程序使用时区拦截器,如下所示:

Our Angular 7+ application use a timezone interceptor, which looks like this:

import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent } from '@angular/common/http';
import { Observable } from 'rxjs';
import { Injectable } from '@angular/core';

@Injectable()
export class TimezoneInterceptor implements HttpInterceptor {

  constructor() {
  }

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;

    if (timezone) {
      req = req.clone({
        setHeaders: {
          Timezone: timezone
        }
      });
    } else {
      console.error('Unknown Timezone!');
    }

    return next.handle(req);
  }

}

Internet Explorer 11不支持 resolvedOptions().timeZone; ,我不知道该怎么用才能使它在所有浏览器中都能正常工作

The Internet Explorer 11 doesn't support the resolvedOptions().timeZone; and I don't know what could I use instead to make it work across all browsers.

我们非常感谢您的帮助!

Every help is appreciated!

推荐答案

实际上,IE 11不支持此功能.请参见

Indeed, this isn't supported on IE 11. See the Intl Compatibility Chart, under "DateTimeFormat" then "resolvedOptions().timeZone defaults to the host environment".

如果必须支持IE 11,则需要采用较早的猜测用户时区的方法.长期以来, jsTimeZoneDetect 是唯一可靠的方法.但是,它没有得到维护,所以这些天我不推荐它.

If you must support IE 11, then you'll need to resort to older methods of guessing the user's time zone. For a long time, jsTimeZoneDetect, was the only reliable method for this. However, it's not been maintained so I wouldn't recommend it these days.

稍后,时刻-时区添加了 moment.tz.guess()功能.由于仍然可以在一定程度上保持这种状态,因此这可能是您唯一的选择.

Later, Moment-Timezone added the moment.tz.guess() function. Since this is still maintained to a certain degree, that's probably your only option.

这两个库都将在可用的情况下使用 Intl API,但随后会使用一种算法,该算法在某些关键DST过渡日期针对各种偏移量查询 Date 对象.这就是为什么它是猜测".通常它非常准确,或至少接近,但在某些情况下无法始终识别用户计算机的确切设置.

Both libraries will use the Intl API when available, but then fall back to an algorithm that interrogates the Date object for various offsets at certain key DST transition dates. This is why it's a "guess". It is usually pretty accurate, or at least close, but cannot always identify the exact setting of the user's computer in certain edge cases.

另一个缺点是这些库必须将时区数据发送到浏览器,这会带来该数据大小的开销.通过使用其中一个截断的数据文件(例如 moment-timezone-with-data-10-year-range.min.js ),可以在某种程度上缓解这种情况.

The other drawback is that these libraries have to ship time zone data to the browser, which comes with the overhead of that data size. This is mitigated somewhat by using one of the truncated data files, such as moment-timezone-with-data-10-year-range.min.js.

如果您不必支持IE 11和旧版本的浏览器,则可以直接使用 Intl API,如您在问题中所显示的那样.

If you don't have to support IE 11 and older browsers, then you can just use the Intl API directly, as you showed in your question.

这篇关于Internet Explorer 11计算的时区错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆