我正在使用moment js,想检查用户在输入框中输入的年份。我的代码如下所示。
const year = moment('10/12/20', 'DD/MM/YYYY').format("YYYY");
year
返回的年份是 2020
而不是 0020
。有谁知道如何防止 moment 变得“太聪明”?
我正在使用版本 "moment": "2.17.1"
。另外,可能与此有关 issue .
最佳答案
使用由单个 Y 组成的年份标记,如下所示:
const year = moment('10/12/20', 'DD/MM/Y').format("YYYY");
YYYY
from version 2.10.5 supports 2 digit years, and converts them to a year near 2000 (same asYY
).
Y
was added in 2.11.1. It will match any number, signed or unsigned. It is useful for years that are not 4 digits or are before the common era. It can be used for any year.
关于javascript - 当年份设置为20年时防止Moment js猜年份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43139494/