为什么Date接受负值? [英] Why does Date accept negative values?

查看:157
本文介绍了为什么Date接受负值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 MDN



日期对象基于时间值,即自UTC时间1970年1月1日以来的毫秒数。



那为什么它接受负值?



即使它确实不应该在1970年1月1日之前的负值平均值?

 新日期('0000','00',' -  1'); //1899-12-30T05:00:00.000Z
新日期('0000','00','00'); //1899-12-31T05:00:00.000Z
新日期(' - 9999','99','99'); //-009991-07-08T04:00:00.000Z

发生什么事了?



更新



对于某些正值,年份从1900年开始

  new Date(100); //1970-01-01T00:00:00.100Z//它表示100Z 
新日期(0100); //1970-01-01T00:00:00.064Z//它表示64Z
新日期(0006,06,06); //1906-07-06T04:00:00.000Z

另请注意,在最后一,日期显示为4,这是错误的。



我怀疑这是某种Y2K错误?!!

解决方案

< blockquote>

这很难且不一致,是的。 JavaScript Date对象是基于 Java 1.0中的一个,它是如此糟糕 Java 重新设计一个全新的包。
JavaScript不太幸运。





  1. 日期是基于 unix eopch 因为它是如何已定义。这是内部详细信息。

  2. 1970年1月1日是此基线的实际时间。

  3. ,因为是时间戳值的方向:+ v的前进,-ve的后退。

  4. 外部,Date构造函数有几种不同的用法,基于参数:



零参数=当前时间



  new Date()//当前日期时间。每个教程都应该教这个。 




时间是绝对的,但'显示'时区可能是UTC或本地。



为简单起见,此答案仅使用UTC。 测试时请记住时区。




一个数字参数=时间戳@ 1970



 新日期(0)// 0ms来自1970-01-01T00:00:00Z。 
新日期(100)//从1970年基线开始的100毫秒。
新日期(-10)//从1970年基线起-10兆。



一个字符串参数= iso日期字符串



 新日期( '000')//短年无效,需要至少四位数。 
新日期('0000')// 0000-01-01。有效,因为有四位数。
new Date('1900')// 1900-01-01。
new Date('1900-01-01')//与上述相同。
new Date('1900-01-01T00:00:00')//与上述相同。
new Date(' - 000001')// 2 BC,见下文。是的,你需要所有那些零。



两个或多个参数=年,月等等@ 1900或0000



 新日期(0,0)// 1900-01-01T00:00:00Z。 
new Date(0,0,1)//与上述相同。日期为1。
新日期(0,0,0)// 1900年前1天= 1899-12-31。
new日期(0,-1)// 1900年前的1个月= 1899-12-01。
new Date(0,-1,0)// 1900年前1个月和1天= 1899-11-30。
new日期(0,-1,-1)// 1个月和1900年前的* 2 *天= 1899-11-29。
new Date('0','1')// 1900-02-01。两个+参数总是投射到年和月。
new Date(100,0)// 0100-01-01。年份> 99使用年份0而不是1900.
new Date(1900,0)// 1900-01-01。与新日期(0,0)相同。太直观了!



负年= BC



 新日期(-1,0)// 0000-01-01前1年= 1 BC前1年= 2 BC。 
新日期(-1,0,-1)//公元前2天前2天。好玩,是吗?我会留下这个练习。




没有0 AC。有1 AC和公元前1年的前一年。 0年级按惯例为1 BC。



2 BC显示为年 - 000001
额外的零是必需,因为它超出正常范围(0000到9999)。
如果您新日期(12345,0)您将获得+ 012345-01-01也是。



当然,格里高利历,直到1923年在欧洲采用,在我们到达BC之前很久就不再有意义了。
事实上,学者们接受这一点耶稣并非出生于公元前一世纪。
但是明星地球以这种规模移动,日历是你最不担心的。







剩余的给定代码只是这些情况的变体。例如:

  new Date(0100)//一个数字=纪元。 0100(八进制)=自1970年以来的64ms 
new Date('0100')//一个字符串= iso = 0100-01-01。
new Date(-9999,99,99)// BC 1之前的9999年然后再加上99个月和98天

希望你玩得开心。请不要忘记投票。 :)


要保持理智,请将所有日期保留在ISO 8601中并使用字符串构造函数。

如果您需要处理时区,请将所有日期时间保留为UTC。



As per MDN

"Date objects are based on a time value that is the number of milliseconds since 1 January, 1970 UTC."

Then why does it accept negative values ?

Even if it did shouldn't negative value mean values before Jan 1, 1970 ?

new Date('0000', '00', '-1');   // "1899-12-30T05:00:00.000Z"
new Date('0000', '00', '00');   // "1899-12-31T05:00:00.000Z"
new Date('-9999', '99', '99');  // "-009991-07-08T04:00:00.000Z"

What is happening ?

Update

For some positive values , the year begins from 1900

new Date(100);                  // "1970-01-01T00:00:00.100Z"   // it says 100Z
new Date(0100);                 // "1970-01-01T00:00:00.064Z"   // it says 64Z
new Date("0006","06","06");     // "1906-07-06T04:00:00.000Z"

Also note that, in the last one, the date is shown as 4 which is wrong.

I suspect this is some sort of Y2K bug ?!!

解决方案

This is hard and inconsistent, yes. The JavaScript Date object was based on the one in Java 1.0, which is so bad that Java redesigned a whole new package. JavaScript is not so lucky.

  1. Date is "based on" unix eopch because of how it is defined. It's internal details.
  2. 1st Jan 1970 is the actual time of this baseline.
  3. since is the direction of the timestamp value: forward for +ve, backward for -ve.
  4. Externally, the Date constructor has several different usages, based on parameters:

Zero parameters = current time

new Date()   // Current datetime. Every tutorial should teach this.

The time is absolute, but 'displayed' timezone may be UTC or local.

For simplicity, this answer will use only UTC. Keep timezone in mind when you test.

One numeric parameter = timestamp @ 1970

new Date(0)    // 0ms from 1970-01-01T00:00:00Z.
new Date(100)  // 100ms from 1970 baseline.
new Date(-10)  // -10ms from 1970 baseline.

One string parameter = iso date string

new Date('000')        // Short years are invalid, need at least four digits.
new Date('0000')       // 0000-01-01.  Valid because there are four digits.
new Date('1900')       // 1900-01-01.
new Date('1900-01-01') // Same as above.
new Date('1900-01-01T00:00:00') // Same as above.
new Date('-000001')    // 2 BC, see below. Yes you need all those zeros.

Two or more parameters = year, month, and so on @ 1900 or 0000

new Date(0,0)      // 1900-01-01T00:00:00Z.
new Date(0,0,1)    // Same as above.  Date is 1 based.
new Date(0,0,0)    // 1 day before 1900 = 1899-12-31.
new Date(0,-1)     // 1 month before 1900 = 1899-12-01.
new Date(0,-1,0)   // 1 month and 1 day before 1900 = 1899-11-30.
new Date(0,-1,-1)  // 1 month and *2* days before 1900 = 1899-11-29.
new Date('0','1')  // 1900-02-01. Two+ params always cast to year and month.
new Date(100,0)    // 0100-01-01. Year > 99 use year 0 not 1900.
new Date(1900,0)   // 1900-01-01. Same as new Date(0,0). So intuitive!

Negative year = BC

new Date(-1,0)    // 1 year before 0000-01-01 = 1 year before 1 BC = 2 BC.
new Date(-1,0,-1) // 2 days before 2 BC.  Fun, yes?  I'll leave this as an exercise.

There is no 0 AC. There is 1 AC and the year before it is 1 BC. Year 0 is 1 BC by convention.

2 BC is displayed as year "-000001". The extra zeros are required because it is outside normal range (0000 to 9999). If you new Date(12345,0) you will get "+012345-01-01", too.

Of course, the Gregorian calendar, adopted as late as 1923 in Europe, will cease to be meaningful long before we reach BC. In fact, scholars accept that Jesus wasn't born in 1 BC. But with the stars and the Earth moving at this scale, calendar is the least of your worries.


The remaining given code are just variations of these cases. For example:

new Date(0100)          // One number = epoch. 0100 (octal) = 64ms since 1970
new Date('0100')        // One string = iso = 0100-01-01.
new Date(-9999, 99, 99) // 9999 years before BC 1 and then add 99 months and 98 days

Hope you had some fun time. Please don't forget to vote up. :)

To stay sane, keep all dates in ISO 8601 and use the string constructor.
And if you need to handle timezone, keep all datetimes in UTC.

这篇关于为什么Date接受负值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆