[英]Sort with moment.js: Deprecation warning: value provided is not in a recognized RFC2822 or ISO format
I parse dates I get from an API with Moment, and I need to sort the array when I'm done collecting the data. 我解析了从带有Moment的API获得的日期,我需要在收集数据时对数组进行排序。 I currently have this:
我目前有这个:
myobject.name = name;
myobject.time = Moment(ajaxinfo.startdate).format('DD/MM/YYYY');
array.push(myobject);
// ... more data is added ...
array.sort((left, right) => {
return Moment.utc(left.time).diff(Moment.utc(right.time));
});
ajaxinfo.startdate
is a string that I get from an API, and it looks like "2018-01-28T13:00:00+00:00"
ajaxinfo.startdate
是我从API获得的字符串,它看起来像"2018-01-28T13:00:00+00:00"
But the above code doesn't work. 但上面的代码不起作用。 It gives me a warning:
它给了我一个警告:
Deprecation warning: value provided is not in a recognized RFC2822 or ISO format.
弃用警告:提供的值不是公认的RFC2822或ISO格式。
moment
construction falls back to jsDate()
, which is not reliable across all browsers and versions.moment
构造回落到jsDate()
,这在所有浏览器和版本中都不可靠。 Non RFC2822/ISO date formats are discouraged and will be removed in an upcoming major release.不鼓励使用非RFC2822 / ISO日期格式,并将在即将发布的主要版本中删除。 Please refer to http://momentjs.com/guides/#/warnings/js-date/ for more info.
有关更多信息,请参阅http://momentjs.com/guides/#/warnings/js-date/ 。
How can I make it work? 我怎样才能使它工作?
As stated by others the format "DD/MM/YYYY" is not an ISO 8601 format and the resulting strings can be ambiguous. 如其他人所述,格式“DD / MM / YYYY”不是ISO 8601格式,结果字符串可能不明确。
You should really work with Date or moment objects, not strings. 您应该使用Date或moment对象,而不是字符串。
So don't call format
when you store the dates in your array objects. 因此,在数组对象中存储日期时不要调用
format
。 If ever you need to render the date, call format
at that very time. 如果您需要渲染日期,请在此时调用
format
。
const Moment = moment; // Sample strings var ajaxinfos = [ { name: "x", startdate: "2018-01-28T13:00:00+00:00" }, { name: "y", startdate: "2018-01-26T18:00:00+00:00" } ]; const array = []; for (const ajaxinfo of ajaxinfos) { const myobject = {}; myobject.name = ajaxinfo.name; myobject.time = Moment(ajaxinfo.startdate); // don't call format array.push(myobject); } // No more need to convert strings to dates while sorting: array.sort((left, right) => left.time.diff(right.time)); console.log(array); // Whenever you need to format: const formatted = array.map(info => info.time.format("MM/DD/YYYY")); console.log(formatted);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.0/moment.min.js"></script>
Based on the link to the momentjs docs in the warning, browsers may be unreliable or inconsistent in parsing the format you're providing "DD/MM/YYYY"
. 根据警告中momentjs文档的链接,浏览器在解析您提供的格式
"DD/MM/YYYY"
可能不可靠或不一致。 If you need to keep this format, one solution would be to provide the format when converting the string back to a moment object when calculating the diff
. 如果你需要保留这种格式,一种解决方案是在计算
diff
时将字符串转换回moment对象时提供格式。 So you could do return moment.utc(left.timeStamp, 'DD/MM/YYYY').diff(moment.utc(right.timeStamp, 'DD/MM/YYYY'))
所以你可以
return moment.utc(left.timeStamp, 'DD/MM/YYYY').diff(moment.utc(right.timeStamp, 'DD/MM/YYYY'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.