Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does new Date(1970, 0 ,1).getFullYear() return 1969?

Tags:

javascript

Can someone explain why new Date(1970, 0 ,1).getFullYear() returns 1969 and not 1970?

result.textContent = new Date(1970, 0, 1).getFullYear();
<div id=result></div>

Filed Firefox Bug: https://bugzilla.mozilla.org/show_bug.cgi?id=1093130

like image 736
wilsonpage Avatar asked Nov 03 '14 15:11

wilsonpage


People also ask

What does getFullYear return?

getFullYear() The getFullYear() method returns the year of the specified date according to local time.

What does new Date () return?

"The expression new Date() returns the current time in internal format, as an object containing the number of milliseconds elapsed since the start of 1970 in UTC.

What is getFullYear () in JavaScript?

Description. Javascript date getFullYear() method returns the year of the specified date according to local time. The value returned by getFullYear() is an absolute number. For dates between the years 1000 and 9999, getFullYear() returns a four-digit number, for example, 2008.

How to get year from new Date js?

JavaScript Date getFullYear() getFullYear() returns the full year (4 digits) of a date.


1 Answers

Looks a lot like a timezone bug in Firefox's SpiderMonkey engine (most likely in some library it uses); in my experimentation it only affects *nix OSes, not Windows. (See below for why timezones come into it.) It's returning

Wed Dec 31 1969 23:00:00 GMT+0000 (BST)

...when of course, we (@wilsonpage, myself, and millions others on UK time) aren't on British Summer Time anymore (and when we are on summer time, it's GMT+0100, not GMT-0100, so it wouldn't make sense for local time to be behind UTC in any case). (Side note: Chrome also shows "BST", but it has the correct date/time.) (Note for Americans: You're used to "xST" meaning "x standard time" [as opposed to "x daylight time"], but here BST stands for "British Summer Time" — e.g., daylight savings time. When we're not on summer time, we're in GMT.)

I see this behavior in Firefox 30 on Linux Mint 16 (apparently the latest in the packages system) and Firefox 33 on that same system (just downloaded and installed from Mozilla directly); I do not see it on Firefox 33 on Windows 8.1.

@wilsonpage has confirmed that he's using OS X and that he's seeing the same time I am (including the "BST" part).

result.textContent = new Date(1970, 0, 1).toString();
<div id=result></div>

The reason timezones come into it (and apparently trigger the bug) is that the values you give that version of the Date constructor are interpreted as UTC, but getFullYear returns its value in local time.

like image 117
7 revs Avatar answered Sep 25 '22 19:09

7 revs