moment.utc-关于无效的输入字符串格式的警告

时间:2018-06-22 10:48:03

标签: momentjs

在测试使用moment.utc的代码时,我有一些测试使用的格式错误:moment.utc('13-06-2018T13:00:00')。这样做时,我会收到以下警告:

Deprecation warning: 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.

那是为什么?我猜想moment.utc是在后台使用UTC格式的简写,并且我看不到接受格式的方法版本,因此我无法摆脱此警告。

如何使用moment.utc解析有效/无效的utc字符串,这样我不会收到此警告?

1 个答案:

答案 0 :(得分:0)

作为警告和链接的guide,您必须查看moment(String)文档,并使用moment(String, String)来解析非ISO 8601或RFC 2822格式的字符串。

  

从字符串创建时刻时,我们首先检查字符串是否与已知的ISO 8601格式匹配,然后检查字符串是否与RFC 2822日期时间格式匹配,然后返回到new Date(string)的后退,如果找不到已知格式。

     

要获得一致的结果以解析ISO 8601字符串以外的任何内容,应使用String + Format

这里有个例子:

console.log( moment.utc('13-06-2018T13:00:00', 'DD-MM-YYYY[T]HH:mm:ss').format() );
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>