javascript - 当年份设置为20年时防止Moment js猜年份

标签 javascript momentjs

我正在使用moment js,想检查用户在输入框中输入的年份。我的代码如下所示。

const year = moment('10/12/20', 'DD/MM/YYYY').format("YYYY");

year 返回的年份是 2020 而不是 0020。有谁知道如何防止 moment 变得“太聪明”?

我正在使用版本 "moment": "2.17.1"。另外,可能与此有关 issue .

It seems to work here so may be a version issue.

最佳答案

使用由单个 Y 组成的年份标记,如下所示:

const year = moment('10/12/20', 'DD/MM/Y').format("YYYY");

来自momentjs documentation :

YYYY from version 2.10.5 supports 2 digit years, and converts them to a year near 2000 (same as YY).

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/

相关文章:

javascript - 在 Javascript 中读取附加到变量的值

javascript - 时刻JS ("Must to show days in next week")

javascript - 在 JavaScript 中按日期对 DateTime 对象进行分组和求和

javascript - 一个月内的小时数的数学函数

vuejs2 - 我的 laravel./vuejs 应用程序中未使用 momentjs 时出现弃用警告

javascript - 为什么在 forEach 迭代中为变量分配多个值?

javascript - 使用 Javascript 在浏览器中显示 html 代码而不是显示元素的功能

javascript - Uncaught SyntaxError : Unexpected token '.' - how do I catch it?

javascript - PHP 或 JS GET 形式 : How to add a field if checkbox is checked

javascript - 时刻格式返回无效日期