【问题标题】:Moment library is returning different time date format in IE10 but working fine in chromeMoment 库在 IE10 中返回不同的时间日期格式,但在 chrome 中工作正常
【发布时间】:2020-01-29 16:47:50
【问题描述】:

moment.tz("2019-11-03T06:45:01.000Z", "America/Chicago").toDate()

这会在 chrome 和 IE11 中返回 "Nov 3, 2019, 01:45 CDT",但在 IE10 中我得到的输出是
“2019 年 11 月 2 日星期六 20:45:01 CDT”。

我观察到一些奇怪的东西,实际的格式问题从 moment-timezone-js 函数 tz where 开始

out = moment.utc.apply(null, "2019-11-03T06:45:01.000Z");

编辑 - 我稍后看到的控制台错误: 尚未为此对象提供区域设置数据

【问题讨论】:

  • toDate 为您获取一个本地 Date 对象,因此当您隐式调用其 toString 方法时,您将获得默认的“格式”,这在不同的浏览器中完全不同。如果您想在任何地方使用特定格式 - 然后使用 moment 的格式化方法来获取它,而不是依赖隐式功能。
  • 所以就像我只想在 DST 不明确时才显示时区,在所有其他情况下我不想显示时区,所以我想使用 toDate()

标签: javascript momentjs timezone-offset timestamp-with-timezone moment-timezone


【解决方案1】:

发生这种情况是因为 IE10 中没有加载语言环境数据,通过将语言环境动态导入并添加到浏览器中得到解决。

解决我的问题的代码正在使用:

const locale = navigator.language
        || 'en-US';
const localeWithoutRegionCode = locale.toLowerCase().split('-')[0];
addLocaleData(require( `intl/locale-data/jsonp/${localeWithoutRegionCode}`));

【讨论】:

    猜你喜欢
    • 2015-11-20
    • 1970-01-01
    • 2017-10-06
    • 2018-02-13
    • 2015-08-26
    • 2019-03-04
    • 2016-01-16
    • 2020-08-19
    • 1970-01-01
    相关资源
    最近更新 更多