为什么 Javascript 将 00 的两位数年份评估为 1900 而不是 2000?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8681534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does Javascript evaluate a 2-digit year of 00 as 1900 instead of 2000?
提问by Rachel
I have an old web app where Javascript is used to validate some dates. Users usually use 2-digit years and I recently discovered it was evaluating 00 as 1900 instead of 2000
我有一个旧的网络应用程序,其中 Javascript 用于验证某些日期。用户通常使用 2 位数年份,我最近发现它将 00 评估为 1900 而不是 2000
if (new Date(tb[0].value) > new Date(tb[1].value)){
alert('Starting date must come before the ending date');
tb[0].focus();
return false;
}
Entering 1/1/99 in the first box and 1/1/00 in the 2nd will cause an error message saying the start date has to be before the end date because 99 is evaluating at 1999 while 00 is evaluating at 1900.
在第一个框中输入 1/1/99 和在第二个框中输入 1/1/00 将导致一条错误消息,指出开始日期必须在结束日期之前,因为 99 的计算时间为 1999,而 00 的计算时间为 1900。
Of course, Users can get around this using 4-digit years, but I still want to know what can be done to get Javascript to evaluate 2-digit years correctly.
当然,用户可以使用 4 位数年份来解决这个问题,但我仍然想知道如何让 Javascript 正确评估 2 位数年份。
So my question is, how can I get Javascript to evaluate 00 as 2000 and not 1900?
所以我的问题是,如何让 Javascript 将 00 评估为 2000 而不是 1900?
采纳答案by Pointy
It does that because the language was created in the 1990's (and in a hurry). You can use getFullYear()
and setFullYear()
to handle years in a non-goofy way.
之所以这样做,是因为该语言是在 1990 年代创建的(并且很匆忙)。您可以使用getFullYear()
和setFullYear()
以一种非愚蠢的方式处理年份。
What I've done is write some code to check for year values less than 100, and if it's greater than 90 (or something similarly appropriate, depending on the situation) assume it's in the 20th century, otherwise assume the 21st.
我所做的是编写一些代码来检查小于 100 的年份值,如果它大于 90(或类似的适当值,取决于情况)假设它在 20 世纪,否则假设是 21 世纪。
And @Rachel no there's no way to tell the runtime library to behave differently, at least not any standardized way. That's just how the Date code works.
而且@Rachel 不,没有办法告诉运行时库以不同的方式运行,至少没有任何标准化的方式。这就是日期代码的工作原理。
回答by Thor84no
The simplest way is just to accept it does it that way and check for it.
最简单的方法就是接受它这样做并检查它。
if (date.getFullYear() < 1970) {
date.setFullYear(date.getFullYear() + 100);
}
1970 is of course just an example value as you have to have a sensible break point. You may want to do that as current year - x instead of a constant of course.
1970 当然只是一个示例值,因为您必须有一个合理的断点。当然,您可能希望将其作为当前年份 - x 而不是常数。
回答by ERR
Chrome actually handles this correctly, but IE and Firefox (at least) do not. Here's my solution:
Chrome 实际上正确处理了这个问题,但 IE 和 Firefox(至少)没有。这是我的解决方案:
var x = new Date(input); //parse the date initially
if (x!="Invalid Date") {
var n = input.split(/[/-]/); //regex to look for / or - delimited dates
if (n[2].length == 2) //if the input has a 2 digit year
{
var y = x.getFullYear();
if (y < 1950) //and the parser decided it's before 1950
x.setFullYear(y + 100); //add a century
}
}
output = dateToShortString(x); //custom function to standardize formatting
回答by Matt H
The way I've done this in the past is to select an arbitrary year that lets the code assume that 2 digit years prior to that arbitrary year are in the 1900's, while years after that are in the 2000's. For an accounting app I had to make Y2K compliant, if I recall correctly, I chose 1940. So transactions between 40-99 were 1940-1999, and transactions 00-39 were 2000-2039.
我过去这样做的方法是选择一个任意年份,让代码假设该任意年份之前的 2 位数年份是 1900 年代,而之后的年份是 2000 年代。对于会计应用程序,我必须让 Y2K 兼容,如果我没记错的话,我选择了 1940。所以 40-99 之间的交易是 1940-1999,交易 00-39 是 2000-2039。
回答by tstrand66
is there a reason you couldn't do something along these lines? The big assumption being that if the user is entering a 2 digit year that its probably not intended to be over 100 years in the past.
有什么原因你不能按照这些方式做一些事情吗?一个重要的假设是,如果用户输入一个 2 位数的年份,那么它可能不打算在过去超过 100 年。
myDate('2-1-00');
function myDate(date) {
let today = new Date();
date = new Date(date.split('-').join('/'));
if ((today.getFullYear() - date.getFullYear()) >= 100) {
date.setFullYear(date.getFullYear() + 100);
}
alert(date);
}