fromNow 在不同的浏览器上显示不同的字符串

fromNow displays different strings on different browsers

我将日期字符串 2015/05/14-02:55 pm 传递给 moment.js 构造函数,我正在使用 fromNow 将其转换为可读格式。当我在 5 月 16 日 运行 此命令时,chrome 显示正确的输出,但 mozilla 显示 a few seconds ago

firefox 控制台输出:

chrome 控制台输出:

这是我的 mozilla 时间设置的问题吗?我在 mozilla 控制台上 运行 Date()。它正确地将日期打印为:

"Sat May 16 2015 15:18:02 GMT+0530 (IST)"

您正在从字符串创建 moment,它是 not recommended:

Warning: Browser support for parsing strings is inconsistent. Because there is no specification on which formats should be supported, what works in some browsers will not work in other browsers.

For consistent results parsing anything other than ISO 8601 strings, you should use String + Format.

只需将格式作为第二个参数传递,它就会在任何浏览器上正确显示:

moment('2015/05/14-02:55 pm', 'YYYY/MM/DD-HH:mm A').fromNow()