此代码在 chrome 上运行良好,但在 firefox 或 IE 上不起作用。 请注意,当我删除“+00:00”时它工作正常
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.11.0/moment.min.js"></script>
<script>
var now = moment("Sun Jun 01 2016 05:00:00+00:00").format('ddd MMM DD YYYY HH:mm:ssZ');
alert(now);
</script>
最佳答案
如果您不传递要解析的字符串格式,您将依赖 moment.js 猜测格式,如果它无法获得看起来像有效日期的内容 (它可能仍然不正确),它将回退到由内置的 Date 构造函数解析,强烈建议不要这样做。 format 方法用于输出格式,而不是解析格式。
要将字符串的格式传递给 moment.js 解析器,请将其作为第二个参数包含在内:
moment("Sun Jun 01 2016 05:00:00+00:00",'ddd MMM DD YYYY HH:mm:ssZ').format(/*output format*/)
另请注意,在回退到内置解析的地方,控制台中会显示以下警告:
Deprecation warning: moment construction falls back to js Date. This is discouraged and will be removed in upcoming major release. Please refer to https://github.com/moment/moment/issues/1407 for more info.
关于javascript - Firefox 上的 moment.js 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37603242/