Moment: Momemtjs 日期转换错误引发。

创建于 2016-10-08  ·  18评论  ·  资料来源: moment/moment

问题描述和重现步骤:
console.log('>>>>>>.', moment(value).format('MM-DD-YYYY'),moment(value).format('YYYY-MM-DD'));

我不确定这个错误是在哪里产生的,无法追踪。 我添加了警告信息。

环境:
Ubuntu,铬浏览器。

解决问题

弃用警告:提供的值不是可识别的 ISO 格式。 moment 构造回退到 js Date(),这在所有浏览器和版本中并不可靠。 不鼓励使用非 ISO 日期格式,并将在即将发布的主要版本中删除。 请参阅http://momentjs.com/guides/#/warnings/js -date/ 了解更多信息。
论据:
[0] _isAMomentObject: true, _isUTC: false, _useUTC: false, _l: undefined, _i: 27/04/2016, _f: undefined, _strict: undefined, _locale: [object Object]
错误

最有用的评论

moment(new Date("27/04/2016")).format....似乎有效。 但我认为它们是这种方法中的一些问题,可能与语言环境等有关。

所有18条评论

当您从不受支持的 ISO 8601 格式的字符串创建 Moment 时,会出现此警告消息(有关支持的格式列表,请参阅 http://momentjs.com/docs/#/parsing/string/)。

在这种情况下,您似乎试图解析不是 ISO 格式的字符串27/04/2016 。 如果您告诉 Moment 格式是什么,则可以在没有警告的情况下解析此格式: moment("27/04/2016", "DD/MM/2016") 。 或者,您可以更改输入格式以匹配 ISO 规范: moment("2016-04-27")

我正在关闭此问题,因为它不是 Moment 中的错误,但如果您仍有问题,请在此处回复中提及我,我会尽力提供帮助。

当我使用受支持的 ISO 8601 格式时,我没有成功

moment(new Date("27/04/2016")).format....似乎有效。 但我认为它们是这种方法中的一些问题,可能与语言环境等有关。

@ziaulrehmandevsinc为我工作。 谢谢。

@ziaulrehmandevsinc也为我工作,谢谢

@ziaulrehmandevsinc它也对我有用。 太感谢了

为我工作。 谢谢

目前,我们不希望调整代码来处理这个问题。 在显示警告的 moment.js 中:

功能警告(味精){
if (hooks.suppressDeprecationWarnings === false &&
(typeof console !== 'undefined') && console.warn) {
console.warn('弃用警告:' + msg);
}

如何设置“hooks.suppressDeprecationWarnings”? 我查看了 hooks 函数,但它报告 hooks 不是函数。 使用奥雷利亚。

谢谢

@pdesimone

// Suppress the warnings
const moment = require('moment');
moment.suppressDeprecationWarnings = true;

// See what else you can mess with
console.log(Object.keys(require('moment')));

是的,我使用moment('2018-01-20')遇到了这个问题,据我所知,这是一种有效的 ISO 8601 格式。

当时,我无法让 supressDepreciationWarnings 或其他解决方案发挥作用。

我在所有的时刻都这样做了:

时刻(值,格式),即:值:2018-01-20,格式:YYYY-MM-DD。

然后警告不会发生。 显然,如果你让时刻“尝试”并找出日期的类型,你会收到警告......

@ziaulrehman40为我工作,非常感谢

如果您认为自己处理正确并且仍然看到错误,请仔细检查您的输入类型<input type="date" />会抛出错误,但<input type="text" />有效。

@pdesimone

// Suppress the warnings
const moment = require('moment');
moment.suppressDeprecationWarnings = true;

// See what else you can mess with
console.log(Object.keys(require('moment')));

谢谢:它工作正常,但我需要知道它是否会在添加抑制弃用警告时引起任何问题

谢谢, @ziaulrehman40 - :kissing_heart:

而不是抑制警告,将日期对象转换为 ISO 字符串,然后再使用 moment 格式化它

 const moment = require('moment');

const dateFormatString = "YYYY-MM-DD";

function formatDate(dateObject) {
    var formatted = moment(dateObject.**toISOString()**).format(dateFormatString);
    return formatted;
}
var d = new Date();
var fd = formatDate(d);

console.log(fd);

在 Repl.it 上进行测试

我也有这个问题。

console.log(moment(this.entities[i].created_at).isValid());
this.entities[i].created_at = moment(this.entities[i].created_at).format("DD-MM-YYYY HH:mm");

控制台日志返回true ,但我收到错误消息:

value provided is not in a recognized RFC2822 or ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non RFC2822/ISO date formats are discouraged and will be removed in an upcoming major release. Please refer to http://momentjs.com/guides/#/warnings/js-date/ for more info.
Arguments: 
[0] _isAMomentObject: true, _isUTC: false, _useUTC: false, _l: undefined, _i: 21-05-2020 19:43, _f: undefined, _strict: undefined, _locale: [object Object]

我收到日期的 console.log 返回2020-05-21 19:43:58.099745+00
有什么建议?

@kiocosta抱歉,您遇到了麻烦。 您可能应该使用格式进行解析。 https://momentjs.com/docs/#/parsing/string -format/ 这对Stack Overflow来说也是一个好问题。

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

vbullinger picture vbullinger  ·  3评论

benhathaway picture benhathaway  ·  3评论

alvarotrigo picture alvarotrigo  ·  3评论

BCup picture BCup  ·  3评论

chitgoks picture chitgoks  ·  3评论