Why does Javascript getYear() return 108?

2019-01-03 15:12发布

Why does this javascript return 108 instead of 2008? it gets the day and month correct but not the year?

myDate = new Date();
year = myDate.getYear();

year = 108?

14条回答
Deceive 欺骗
2楼-- · 2019-01-03 16:11

As others have said, it returns the number of years since 1900. The reason why it does that is that when JavaScript was invented in the mid-90s, that behaviour was both convenient and consistent with date-time APIs in other languages. Particularly C. And, of course, once the API was established they couldn't change it for backwards compatibility reasons.

查看更多
甜甜的少女心
3楼-- · 2019-01-03 16:11

it is returning 4 digit year - 1900, which may have been cool 9+ years ago, but is pretty retarded now. Java's java.util.Date also does this.

查看更多
登录 后发表回答