【问题标题】:Is this a bug in IE 11? [duplicate]这是 IE 11 中的错误吗? [复制]
【发布时间】:2019-08-09 11:15:01
【问题描述】:

console.log(
  ('00' + (new Date('3/4/2019')).toLocaleDateString("en-US").split('/')[1]).slice(-2)
);

IE:

"4"

"04"

解决方法是什么?

【问题讨论】:

  • 您为什么认为04 正确?我不把星球大战日写成 5 月 4 日,而是 5 月 4 日。
  • @Garfield,您的问题是否仍然存在,或者您从社区成员建议的其他线程中获得了解决方案?请让我们知道问题的当前状态。如果需要,我们将尝试提供进一步的建议。感谢您的理解。
  • 重复线程解释 IE11 在 toLocaleDateString() 中添加了隐藏字符,因此 .slice() 不起作用。我的解决方案类似于接受的答案,但在重复线程上也提供了可接受的解决方法。

标签: javascript internet-explorer-11


【解决方案1】:

为什么不直接从对象Date 的可用方法中获取日期。

var day = new Date('3/4/2019').getDate();
console.log(day >= 10 ? day : ("0" + day)); // We can use String.prototype.padStart(2, "0"), however, that's not available in IE11

【讨论】:

    猜你喜欢
    • 2019-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-16
    • 2013-08-09
    • 2012-06-02
    • 1970-01-01
    相关资源
    最近更新 更多