0

If I'm executing this:

var date = new Date("10.31");
date.setFullYear(-125);

the output of date is Sun Oct 31 -125 00:00:00 GMT+0200 (W. Europe Summer Time)

If I check this on wolframalpha the day seems to be tuesday.

Can someone explain why not the same day is displayed by both source?

kevinSpaceyIsKeyserSöze
  • 3,693
  • 2
  • 16
  • 25

3 Answers3

2

The reason for the difference between JavaScript and wolframalpha website is that JavaScript is calculating the years mathematically, so it includes the year zero. Try to set the year to zero in JavaScript and you will see that it works. However, there is no such a thing as year zero, and the year before year 1 is year 1 BC. Try to set the year to zero on wolframalpha website and you get an error, while it automatically converts all negative years to years BC. This is the correct behavior.

To get the BC years in JavaScript, add 1 to every year below 1. So year 0 becomes 1BC, and year -125 becomes 126BC. In JavaScript this gives you Sunday, and 126BC on wolframalpha website gives you Sunday too. 125BC gives you Tuesday on wolframalpha website, and -124 gives you the same in JavaScript.

var date = new Date();
date.setFullYear(-124);
date.setMonth(9);
date.setDate(31);
console.log(date.toString());
date.setFullYear(-125);
console.log(date.toString());
Racil Hilan
  • 24,690
  • 13
  • 50
  • 55
  • `add 1 to every year below 1. So year 0 becomes 1BC, and year -125 becomes 126BC` if I add one year it will be -124 – kevinSpaceyIsKeyserSöze Nov 02 '17 at 08:54
  • 1
    No, I meant adding 1 to the absolute value, so `125+1`, not `-125+1` :). I said *"-124 becomes 125BC"*, not `-125`. The reason is that date values are always positive even for BC dates. There is no year zero and there are no negative years. – Racil Hilan Nov 03 '17 at 18:56
0

Javascript dates start in 1970.

Let's do a quick count.

(new Date()).setYear(-125); //returns -66085584766591 (milliseconds from time 0)
//Let's convert those milliseconds in years...
//-66085584766591 = -2095,56 (years???)

As you can see, you can't rely on negative dates in Javascript.

Luca De Nardi
  • 2,280
  • 16
  • 35
  • They start in 1980? Maybe 1-1-1970? – Racil Hilan Oct 31 '17 at 13:13
  • Date starts in 1970 (Unix epoch): `new Date(0) => Thu Jan 01 1970 01:00:00 GMT+0100 (Paris, Madrid)` – Apolo Oct 31 '17 at 13:13
  • @RacilHilan my bad, hit the wrong number :) corrected. – Luca De Nardi Oct 31 '17 at 13:13
  • This isn't how javascript negative years works, they produce a BC date. – bryan60 Oct 31 '17 at 13:14
  • They might produce a BC date but it isn't reliable for sure. That's just what I'm saying – Luca De Nardi Oct 31 '17 at 13:15
  • 1
    And what’s the issue with the result? 1970 + 125 = 2095, that’s quite correct. – jcaron Oct 31 '17 at 16:48
  • Your claim that negative dates in JavaScript are unreliable is incorrect. Your calculation for the negative value `-66085584766591` equally applies to positive values `66085584766591`, then all dates in JavaScript are unreliable. How did you calculate the value 2095.56? You just did a simple math, right? You forgot that the year is not exactly 365 days, didn't you? JavaScript takes the lap years into consideration when it does the calculation. – Racil Hilan Oct 31 '17 at 17:34
0

negative years in javascript do produce a BC date but it's kind of a poor design. Wolfram Alpha is probably correct. See this answer for more: Why does Date accept negative values?

bryan60
  • 28,215
  • 4
  • 48
  • 65