在JavaScript中使用公元1000年之前的日期的提示 [英] Tips for working with Pre-1000 A.D. Dates in JavaScript
问题描述
我很好奇,如果有人有什么好的解决方案可以准确地确定公元1000年之前的日期,尤其是公元1-100年。
I'm curious if anyone has any good solutions for accurately building dates prior to the year 1000 A.D. - particularly the years 1 - 100 AD.
例如,如果我想为第一个千年的开始建立一个日期,我不能只是...
For example, if I want to build a date for the start of the 1st millenium, I can't just do...
new Date(Date.UTC(1,0,1,0,0,0,0));
因为它试图变得聪明,并假设1是1901,这给了我...
because it tries to be "smart" and assume that 1 is 1901, which gives me...
Sun Dec 31 1900 18:00:00 GMT-0600 (CST)
99年也是一样...
The same thing goes for the year 99...
new Date(Date.UTC(99,0,1,0,0,0,0));
将变为
Thu Dec 31 1998 18:00:00 GMT-0600 (CST)
思路?
推荐答案
这是我想出的基本解决方案。如果某个日期在1000年之前,那么我在构造日期时会向其添加1000,然后再使用 setUTCFullYear()
。
Here is the basic solution I came up with. If a date is prior to year 1000, I just add a 1000 to it while constructing the date, then use setUTCFullYear()
afterwards.
if (year >= 0 && year < 1000) {
var d = new Date(Date.UTC(year + 1000,mon,day,hour,min,sec,0));
d.setUTCFullYear(d.getFullYear() - 1000);
return d;
}
1000可能会过大,因为我只是在100年前的约会中遇到问题。 ..但是,无论如何。
1000 may be overkill since I was only having problems with pre-100 dates... but, whatever.
这篇关于在JavaScript中使用公元1000年之前的日期的提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!