admin管理员组文章数量:1026380
Is the behavior correct when in strict mode, for format M/D/YYYY
, moment.js will accept 01.09.2017
? Shouldn't it accept just 1.9.2017
, if there is the only "one M
" in the format? Same applies for D
days.
Edit:
moment('1/09/2017', 'MM/D/YYYY', true).isValid() // false
moment('01/09/2017', 'M/D/YYYY', true).isValid() // true -> why?
Why is M
benevolent for 01
, but MM
is not for 1
? Is it a bug?
Is the behavior correct when in strict mode, for format M/D/YYYY
, moment.js will accept 01.09.2017
? Shouldn't it accept just 1.9.2017
, if there is the only "one M
" in the format? Same applies for D
days.
Edit:
moment('1/09/2017', 'MM/D/YYYY', true).isValid() // false
moment('01/09/2017', 'M/D/YYYY', true).isValid() // true -> why?
Why is M
benevolent for 01
, but MM
is not for 1
? Is it a bug?
- Do you mean strict mode ? – Cody Geisler Commented Mar 13, 2017 at 14:03
- Yes, thanks for correction. – mimo Commented Mar 13, 2017 at 14:07
1 Answer
Reset to default 3It could work not in correct way. In your example it works fine, but in other situation it could make a wrong result. Moment format doc
moment("01.09.2017", "M/D/YYYY").format("M/D/YYYY");
//"1/9/2017"
moment("02-25-1995", "MMM/D/YY").format("MM/DD/YYYY");
//"01/02/2025"
If you need to specify multiple formats, you can do it by set second argument as array of string formats. Moment multiple format
moment("29-06-1995", ["MM-DD-YYYY", "DD-MM", "DD-MM-YYYY"]); // uses the last format
moment("05-06-1995", ["MM-DD-YYYY", "DD-MM-YYYY"]); // uses the first format
For strict mode: this parameter effect to regular expression which parsing data and for number it will work correctly.
//Non strict regular
Hb = /\d\d?/
//Strict regular
Sb = /\d\d/
d = b._strict;
case"MM":
case"DD":
case"YY":
case"GG":
case"gg":
case"HH":
case"hh":
case"mm":
case"ss":
case"ww":
case"WW":
return d ? Sb : Hb;
In case with one letter always use non strict regular
case"M":
case"D":
case"d":
case"H":
case"h":
case"m":
case"s":
case"w":
case"W":
case"e":
case"E":
return Hb;
So strict mode will fails in case
moment("1/9/1990", "MM/MD/YYYY", true).format("M/D/YYYY")
"Invalid date"
All this was test on momentjs version 2.7.0
In new version of moment 2.17.1 strict parameters used in formats ['MMM', 'MMMM'], ['dd', 'ddd', 'dddd']
Is the behavior correct when in strict mode, for format M/D/YYYY
, moment.js will accept 01.09.2017
? Shouldn't it accept just 1.9.2017
, if there is the only "one M
" in the format? Same applies for D
days.
Edit:
moment('1/09/2017', 'MM/D/YYYY', true).isValid() // false
moment('01/09/2017', 'M/D/YYYY', true).isValid() // true -> why?
Why is M
benevolent for 01
, but MM
is not for 1
? Is it a bug?
Is the behavior correct when in strict mode, for format M/D/YYYY
, moment.js will accept 01.09.2017
? Shouldn't it accept just 1.9.2017
, if there is the only "one M
" in the format? Same applies for D
days.
Edit:
moment('1/09/2017', 'MM/D/YYYY', true).isValid() // false
moment('01/09/2017', 'M/D/YYYY', true).isValid() // true -> why?
Why is M
benevolent for 01
, but MM
is not for 1
? Is it a bug?
- Do you mean strict mode ? – Cody Geisler Commented Mar 13, 2017 at 14:03
- Yes, thanks for correction. – mimo Commented Mar 13, 2017 at 14:07
1 Answer
Reset to default 3It could work not in correct way. In your example it works fine, but in other situation it could make a wrong result. Moment format doc
moment("01.09.2017", "M/D/YYYY").format("M/D/YYYY");
//"1/9/2017"
moment("02-25-1995", "MMM/D/YY").format("MM/DD/YYYY");
//"01/02/2025"
If you need to specify multiple formats, you can do it by set second argument as array of string formats. Moment multiple format
moment("29-06-1995", ["MM-DD-YYYY", "DD-MM", "DD-MM-YYYY"]); // uses the last format
moment("05-06-1995", ["MM-DD-YYYY", "DD-MM-YYYY"]); // uses the first format
For strict mode: this parameter effect to regular expression which parsing data and for number it will work correctly.
//Non strict regular
Hb = /\d\d?/
//Strict regular
Sb = /\d\d/
d = b._strict;
case"MM":
case"DD":
case"YY":
case"GG":
case"gg":
case"HH":
case"hh":
case"mm":
case"ss":
case"ww":
case"WW":
return d ? Sb : Hb;
In case with one letter always use non strict regular
case"M":
case"D":
case"d":
case"H":
case"h":
case"m":
case"s":
case"w":
case"W":
case"e":
case"E":
return Hb;
So strict mode will fails in case
moment("1/9/1990", "MM/MD/YYYY", true).format("M/D/YYYY")
"Invalid date"
All this was test on momentjs version 2.7.0
In new version of moment 2.17.1 strict parameters used in formats ['MMM', 'MMMM'], ['dd', 'ddd', 'dddd']
本文标签: javascriptmomentjs accepts two digit month quot01quot when the format is MStack Overflow
版权声明:本文标题:javascript - moment.js accepts two digit month "01" when the format is M - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745649727a2161242.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论