简体   繁体   English

Moment.js - moment()。format()创建无效日期?

[英]Moment.js - moment().format() creates invalid date?

moment().format() creates a date that is not valid according to moment().isValid() moment().format()根据moment().isValid()创建一个无效的日期moment().isValid()

Here is the example: 这是一个例子:

> moment.version
"2.14.1"
> moment.locale()
"fr"
> moment().format("ll")
"29 juill. 2016"
> moment("29 juill. 2016", "ll", true).isValid()
false

However if I remove the period in the month it works: 但是,如果我删除该月的工作时间:

> moment("29 juill 2016", "ll", true).isValid()
true

Or if I disable strict parsing (remove the 3rd parameter) it works: 或者,如果我禁用严格解析(删除第3个参数),它可以工作:

> moment("29 juill. 2016", "ll").isValid()
true

Why is this? 为什么是这样? Why doesn't moment().format("ll") create a date that is valid with strict parsing? 为什么moment().format("ll")创建一个有效的严格解析日期?

I think that's because "ll" is not a valid date formatting, if you will run the moment function and ask for a timestamp ( .valueOf ) you will get a NaN 我认为这是因为"ll"不是一个有效的日期格式,如果你将运行moment函数并要求一个时间戳( .valueOf )你会得到一个NaN

moment("29 juill 2016", "ll", true).valueOf()
// NaN

You need to provide a valid formatting for the 2nd argument, for your date string it would be "DD MMMM, YYYY" 您需要为第二个参数提供有效的格式,对于您的日期字符串,它将是"DD MMMM, YYYY"

Also, I think you had a typo in juill , I think it should be juillet 此外,我认为你在juill有一个错字,我认为它应该是juillet

moment.locale("fr")
// "fr"
 moment("29 july, 2016", "DD MMMM, YYYY", true).isValid()
// false
 moment("29 juillet, 2016", "DD MMMM, YYYY", true).isValid()
// true

Answering this myself in case someone else faces the same issue. 如果其他人面临同样的问题,我自己回答这个问题。

This was due to an issue with moment.js version 2.8.1 not parsing custom short month names with periods correctly. 这是由于moment.js版本2.8.1无法正确解析自定义短月份名称的问题。 This issue is resolved in the later version 2.14.1 . 此问题已在更高版本2.14.1得到解决。

Here's the example that produces different results in 2.8.1 and 2.14.1 这是在2.8.12.14.1中产生不同结果的示例

moment.locale("fr", {
  monthsShort: [
    "janv.",
    "févr.",
    "mars",
    "avr.",
    "Mai",
    "juin",
    "juilltest.",
    "août",
    "sept.",
    "oct.",
    "nov.",
    "déc."
  ],
  monthsParseExact: true,
  longDateFormat: {
    LL: "DD MMM YYYY",
    ll: "DD MMM YYYY"
  },

});
console.log(moment.version);
moment.locale('fr');
console.log(moment.locale());
var testDate = '29 juilltest. 2016'; // month name with period in it that matches the custom short name given above
console.log(moment(testDate, "LL", true).isValid());
console.log(moment(testDate, "ll", true).isValid());
console.log(moment.localeData("fr"));

Version 2.8.1: https://jsfiddle.net/3do4ubsj/ 版本2.8.1: https //jsfiddle.net/3do4ubsj/

2.8.1
fr
false
false

Version 2.14.1: https://jsfiddle.net/pkhcaqmy/ 版本2.14.1: https //jsfiddle.net/pkhcaqmy/

2.14.1
fr
true
true

The commit that fixed it: https://github.com/moment/moment/commit/fc5a352e9ca30e32a96875810604ad981d1442c3 修复它的提交: https//github.com/moment/moment/commit/fc5a352e9ca30e32a96875810604ad981d1442c3

A related issue in moment.js repo: https://github.com/moment/moment/issues/3126 moment.js repo中的相关问题: https//github.com/moment/moment/issues/3126

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM