Javascript 日期 01/01/0001

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29968116/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:24:30  来源:igfitidea点击:

Javascript date 01/01/0001

javascriptdatedatetime

提问by Naramsim

I want to create a Date object in javascript, which represents the year 0001, 2014 years ago.

我想在javascript中创建一个Date对象,它代表2014年前的0001年。

I tried with

我试过

d = new Date(); d.setYear(1);
console.log(d);

but it gives the year 1901

但它给出了 1901 年

with

d = new Date(1,1,1)
console.log(d);

no way.

没门。

How can I create this date?

如何创建此日期?

回答by obenjiro

First of all it's not a Y2K issue at all!( UPDATE: in some cases - it's related to Y2K issues, but it's not the problem here )

首先,这根本不是千年虫问题!(更新:在某些情况下 - 它与 Y2K 问题有关,但这不是这里的问题)

The correct answer is that you can't do that reliably. Does Daylight saving time apply to year 1? How many Leap Years were there? Were there any? Etc. But the answer by @Daniel will use it!

正确答案是你不能可靠地做到这一点。夏令时适用于第 1 年吗?有多少个闰年?有吗?等等。但是@Daniel 的回答会使用它!

UPDATE:not to mention @MattJohnson post about DST. DST in year 1, actually JS (ES5 anyway) will lie and use the current DST rule for all years

更新:更不用说@MattJohnson 关于夏令时的帖子了。第 1 年的 DST,实际上 JS(无论如何都是 ES5)会撒谎并在所有年份使用当前的 DST 规则

So please don't fool yourself with the idea that you can reliably work with dates lower than 1970. (You will have a lot of problems and surprises even in that time range.)

所以请不要自欺欺人地认为您可以可靠地处理低于 1970 年的日期。(即使在那个时间范围内,您也会遇到很多问题和惊喜。)

But if you really, really need to you can use new Date('0001-01-01')(ISO 8601 format) or @Daniel's method:

但如果你真的,真的需要你可以使用new Date('0001-01-01')(ISO 8601 格式)或@Daniel 的方法:

var d = new Date(); d.setFullYear(1);

But before you use it read this...

但是在你使用它之前阅读这个......



There are 4 ways to create a Date in JS:

在 JS 中创建 Date 有 4 种方法:

new Date()
new Date(milliseconds)
new Date(dateString)
new Date(year, month, day, hours, minutes, seconds, milliseconds)


1) new Date()creates current date ( in your local timezone ) so we are not interested in it for now.

1) new Date()创建当前日期(在您当地的时区),所以我们现在对它不感兴趣。



2) new Date(number)creates a new date object as zero time plus the number. Zero time is 01 January 1970 00:00:00 UTC.

2) new Date(number)创建一个新的日期对象作为零时间加上数字。零时间是 1970 年 1 月 1 日 00:00:00 UTC。

So in this case time in JS counting from year 1970.

所以在这种情况下,JS 中的时间从 1970 年开始计算。

(new Date(0)).toUTCString()
"Thu, 01 Jan 1970 00:00:00 GMT

If you use negative number you can go "back" it time before 1970

如果您使用负数,则可以在 1970 年之前“返回”时间

(new Date(-62167219200000)).toUTCString()
"Sat, 01 Jan 0 00:00:00 GMT"
-62167219200000             <- milliseconds
-62167219200000 / 1000
-62167219200             <- seconds
-62167219200 / 60
-1036120320             <- minutes
-1036120320 / 60
-17268672             <- hours
-17268672 / 24
-719528             <- days
-719528 / 365
-1971.309589041096 <- years ( this is roughly calculated value )

The problem is that it's not reliable. How many Leap Years were there? Were there any? Daylight saving time? Etc. And I don't like it because of this magic number -62167219200000

问题是它不可靠。有多少个闰年?有吗?夏令时?等等。我不喜欢它因为这个神奇的数字 -62167219200000



3) new Date(dateString)This is most 'reliable' way. DateString - A string representing an RFC2822 or ISO 8601 date.

3) new Date(dateString)这是最“可靠”的方式。DateString - 表示 RFC2822 或 ISO 8601 日期的字符串。

RFC2822 / IETF date syntax (RFC2822 Section 3.3), e.g. "Mon, 25 Dec 1995 13:30:00 GMT"

RFC2822 / IETF 日期语法(RFC2822 第 3.3 节),例如“Mon, 25 Dec 1995 13:30:00 GMT”

The problem with it is that if you use negative number you will get an incorrect Date with NaN in a result of all methods

它的问题是,如果你使用负数,你会在所有方法的结果中得到一个不正确的日期和 NaN

(new Date('01 January -1 00:00:00 UTC')).getFullYear()
NaN

If you use year higher or equal 0 and lower then 50 then 2000 will be added automatically.

如果您使用高于或等于 0 且低于 50 的年份,则将自动添加 2000。

(new Date('01 January 0 00:00:00 UTC')).getFullYear()
2000
(new Date('01 January 1 00:00:00 UTC')).getFullYear()
2001
(new Date('01 January 10 00:00:00 UTC')).getFullYear()
2010
(new Date('01 January 01 00:00:00 UTC')).getFullYear()
2001
(new Date('01 January 30 00:00:00 UTC')).getFullYear()
2030
(new Date('01 January 49 00:00:00 UTC')).getFullYear()
2049

And if you use year higher or equal 50 and lower then 100 then 1900 will be added.

如果您使用高于或等于 50 且低于 100 的年份,则将添加 1900。

(new Date('01 January 50 00:00:00 UTC')).getFullYear()
1950
(new Date('01 January 51 00:00:00 UTC')).getFullYear()
1951
(new Date('01 January 90 00:00:00 UTC')).getFullYear()
1990
(new Date('01 January 99 00:00:00 UTC')).getFullYear()
1999

Years equal 100 or higher will get correct year number

等于 100 或更高的年份将获得正确的年份编号

(new Date('01 January 100 00:00:00 UTC')).getFullYear()
100
(new Date('01 January 101 00:00:00 UTC')).getFullYear()
101
(new Date('01 January 999 00:00:00 UTC')).getFullYear()
999
(new Date('01 January 9999 00:00:00 UTC')).getFullYear()
9999

So we can't create year 1 using RFC2822 / IETF date syntax

所以我们不能使用 RFC2822 / IETF 日期语法创建第 1 年

About ISO 8601:

关于 ISO 8601:

http://www.w3.org/TR/NOTE-datetime

http://www.w3.org/TR/NOTE-datetime

The actual formats are

实际格式是

Year:
      YYYY (eg 1997)
Year and month:
      YYYY-MM (eg 1997-07)
Complete date:
      YYYY-MM-DD (eg 1997-07-16)
Complete date plus hours and minutes:
      YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00)
Complete date plus hours, minutes and seconds:
      YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)
Complete date plus hours, minutes, seconds and a decimal fraction of a second
      YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)

We are most interested in 'Complete date'

我们最感兴趣的是“完成日期”

(new Date('0001-01-01')).toUTCString()
"Mon, 01 Jan 1 00:00:00 GMT"

Yahoo!!! ( or it is better to say Google! :) ), we can use ISO 8601 to create date with year 1

雅虎!!!(或者说谷歌更好!:)),我们可以使用 ISO 8601 来创建日期为 1

But be careful and do not try to use negative numbers or short year numbers, as parsing of those will may vary on localization or just insanity :)

但是要小心,不要尝试使用负数或短年份数,因为对这些的解析可能会因本地化或精神错乱而有所不同:)

(new Date('-0001-01-01')).toUTCString()
"Sun, 31 Dec 2000 21:00:00 GMT"
(new Date('01-01-01')).toUTCString()
"Sun, 31 Dec 2000 21:00:00 GMT"
(new Date('02-01-01')).toUTCString()
"Wed, 31 Jan 2001 21:00:00 GMT"
(new Date('02-01-05')).toUTCString()
"Mon, 31 Jan 2005 21:00:00 GMT"


4) new Date(year, month, day, hours, minutes, seconds, milliseconds)

4)新日期(年、月、日、时、分、秒、毫秒)

To use this one you have to pass two parameters ( year, and month ), all other will be optional. Be careful because here month will start from 0 to 11, not like everywhere else. WAT? o_O

要使用这个,您必须传递两个参数(年和月),所有其他参数都是可选的。小心,因为这里的月份将从 0 到 11,而不是其他地方。哇?o_o

WARNING!This date will be created in your current time zone!!! So be careful using it!

警告!此日期将在您当前的时区创建!!!所以要小心使用!

UPD:clarification by @matt-johnson

UPD:@matt-johnson 的澄清

...actually the Date object always reflects the local time zone. You can't place it in another time zone, and even if you initialize it with a UTC timestamp, it will still reflect back the local time zone in most of the functions. Internally it tracks UTC by the numeric timestamp, and there are functions that expose UTC values explicitly, but everything else is local.

...实际上 Date 对象总是反映本地时区。你不能把它放在另一个时区,即使你用 UTC 时间戳初始化它,它仍然会在大多数函数中反映本地时区。在内部,它通过数字时间戳跟踪 UTC,并且有一些函数显式公开 UTC 值,但其他一切都是本地的。

The negative numbers will be interpreted as negative years

负数将被解释为负年

(new Date(-1, 0)).toString()
"Fri Jan 01 -1 00:00:00 GMT+0300 (Russia TZ 2 Standard Time)"
(new Date(-234, 0)).toString()
"Wed Jan 01 -234 00:00:00 GMT+0300 (Russia TZ 2 Standard Time)"

The numbers from 0 to 99 will be incremented by 1900 automatically

0 到 99 之间的数字将自动增加 1900

(new Date(0, 0)).toString()
"Mon Jan 01 1900 00:00:00 GMT+0300 (Russia TZ 2 Standard Time)"
(new Date(1, 0)).toString()
"Tue Jan 01 1901 00:00:00 GMT+0300 (Russia TZ 2 Standard Time)"
(new Date(11, 0)).toString()
"Sun Jan 01 1911 00:00:00 GMT+0300 (Russia TZ 2 Standard Time)"
(new Date(50, 0)).toString()
"Sun Jan 01 1950 00:00:00 GMT+0300 (Russia TZ 2 Standard Time)"
(new Date(99, 0)).toString()
"Fri Jan 01 1999 00:00:00 GMT+0300 (Russia TZ 2 Standard Time)"

The numbers from 100 to 275760 will be interpreted as year numbers

从 100 到 275760 的数字将被解释为年份数字

(new Date(100, 0)).toString()
"Fri Jan 01 100 00:00:00 GMT+0300 (Russia TZ 2 Standard Time)"
(new Date(102, 0)).toString()
"Sun Jan 01 102 00:00:00 GMT+0300 (Russia TZ 2 Standard Time)"
(new Date(2002, 0)).toString()
"Tue Jan 01 2002 00:00:00 GMT+0300 (Russia TZ 2 Standard Time)"

And numbers higher then 275760 will be Invalid date

高于 275760 的数字将是无效日期

(new Date(275760, 0)).toString()
"Tue Jan 01 275760 00:00:00 GMT+0300 (Russia TZ 2 Standard Time)"
(new Date(275761, 0)).toString()
"Invalid Date"

UPD:

更新:

new Date(Date.UTC(1,1,1)) will safer from same symptoms as new Date(year, month, day, hours, minutes, seconds, milliseconds). Because of Date.UTC function.

new Date(Date.UTC(1,1,1)) 与 new Date(year, month, day, hours, minute, seconds, milliseconds) 相同的症状会更安全。由于 Date.UTC 功能。

回答by Daniel A. White

Use setFullYear- this is the Y2K issue with JavaScript.

使用setFullYear- 这是 JavaScript 的 Y2K 问题。

var d = new Date(); d.setFullYear(1);
document.write(d);

回答by Salman A

The (deprecated) Date.setYearmethod sets the year for a specified date, but note that:

(deprecated)Date.setYear方法设置指定日期的年份,但请注意:

If yearValue is a number between 0 and 99 (inclusive), then the year for dateObj is set to 1900 + yearValue. Otherwise, the year for dateObj is set to yearValue.

var theBigDay = new Date();

theBigDay.setYear(96);   // sets year to 1996
theBigDay.setYear(1996); // sets year to 1996
theBigDay.setYear(2000); // sets year to 2000

如果 yearValue 是 0 到 99(含)之间的数字,则 dateObj 的年份设置为 1900 + yearValue。否则,dateObj 的年份设置为 yearValue。

var theBigDay = new Date();

theBigDay.setYear(96);   // sets year to 1996
theBigDay.setYear(1996); // sets year to 1996
theBigDay.setYear(2000); // sets year to 2000

You should use Date.setFullYearmethod nevertheless. The other method is deprecated.

Date.setFullYear不过你应该使用方法。另一种方法已弃用。

回答by T.J. Crowder

You can use the string version of the constructor:

您可以使用构造函数的字符串版本:

console.log(new Date("0001-01-01"));

回答by javabot

when handling JavaScript date objects you have to specify the full year for ex, 1950 and not 50. It is mentioned in the doc. So the code should be as Daniel has said also,

在处理 JavaScript 日期对象时,您必须为 ex 指定整年,1950 年而不是 50 年。文档中提到了这一点。所以代码也应该像丹尼尔所说的那样,

d = new Date(); d.setFullYear(1);
console.log(d);