Date difference when year is smaller than zero - javascript

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?

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());

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?

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.

Related

Bug in JavaScript date 1 Jan 0099

On 1 jan 0099 there was Thrusday but it return. Friday
days = new Date(" January 1 ,0099")
day = days.getDay()
alert(day);
RESULT
5
But it should return 4
Basically, it appears Javascript won't construct a Date in the year 99:
year
Integer value representing the year.
Values from 0 to 99 map to the years 1900 to 1999. All other values are the actual year.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date#Syntax
You can try with different formats, 99 always appears to map to 1999. Likely this was implemented as a workaround and/or “convenience” for Y2K dates, perhaps even inherited from Java.
I'm not sure if there's a better workaround, but this works:
let d = new Date(100, 0, 1);
d.setFullYear(99);

What is year 0 in Javascript?

Take the following code
var d = new Date();
d.setFullYear(0);
alert(d);
What year is year 0000? After all, year 0 isn't actually a thing, since we went from 1BC to 1AD. Is year 0 actually 1BC and year -1 actually 2BC?
The ES262 specification says:
20.3.1.3 Year Number
ECMAScript uses a proleptic Gregorian calendar to map a day number to a year number and to determine the month
and date within that year.
If you look up proleptic Gregorian calendar on Wikipedia, you'll find:
For these calendars one can distinguish two systems of numbering years BC. Bede and later historians did not use the Latin zero, nulla, as a year (see Year zero), so the year preceding AD 1 is 1 BC. In this system the year 1 BC is a leap year (likewise in the proleptic Julian calendar). Mathematically, it is more convenient to include a year 0 and represent earlier years as negative, for the specific purpose of facilitating the calculation of the number of years between a negative (BC) year and a positive (AD) year.
Therefore it is up to your interpretation wether year 0 exists or not.

Javascript Date MAX_VALUE

In JavaScript, what is the maximum value for the year in a Date?
How can I find this out in code?
I have tried the following:
new Date().getFullYear().MAX_VALUE;
Thanks in advance.
As per specs here: https://262.ecma-international.org/11.0/#sec-time-values-and-time-range : The actual range of times is 8,640,000,000,000,000 milliseconds to either side of 01 January, 1970 UTC. So, the maximum valid year you will get is 275760 (new Date(8640000000000000).getFullYear()). And to get the minimum valid year, new Date(-8640000000000000).getFullYear() or 271821 B.C.E.

Validate Uk formatted date

I'm trying to validate a uk date using this code:
function ukdate(d) {
var p = new Date(d.split('/')[2], d.split('/')[1] -1, d.split('/')[0]);
if(p.toString() !== 'Invalid Date') {
return p;
}
}
http://jsfiddle.net/GE3xU/1/
so if I try ukdate('31/12/1981') it correctly returns "The Dec 31 1981". However if i try ukdate('12/31/1981') it returns "Tue Jul 12 1983".
Why is this happening? I'm expecting the second test to return invalid date because 31 is not a valid month.
JavaScript is converting your date for you.
In simple examples, you can get the last day of a given month by asking for the 0th day of the following month. Similarly, the "32nd of August" would be corrected to the 1st of September.
Months work similarly. The 13th month of a given year is the 1st month of the next. The 0th month of a year is December of the previous.
31 % 12 = 7, hence July, and floor(31/12) = 2 hence the year being shifted forward by two.
This is intended behaviour for JavaScript.
May I interest you in <input type="date" />? It uses whatever format is defined on the user's computer (ie. it is "locale-aware"), which is already excellent for user experience. On top of that, supporting browsers will render a calendar date picker, especially useful on phones too. Internally, the date is in "standard" YYYY-mm-dd format.
The month value is divided by 12 and added to the year, then the remainder is used as the actual month value.
See the spec
Let ym be y + floor(m /12).
Let mn be m modulo 12.

Date / Time problem in javascript with highcharts

2011-09-13 is today (GMT+0)
Date.UTC(<%= effort.week_commencing.strftime("%Y,%m,%d") %>)
Outputs
Date.UTC(2011,09,12)
This is right because it is getting the 12Th which is the start of the month.
But in high charts / stock charts it is displaying 1 month ahead "12Th of October"
What could be the problem?
Months for Date are enumerated from 0. So Date.UTC(2011,09,12) is really 12th of October and Date.UTC(2011,08,12) will be 12th of September. Just subtract 1 from month value.
try new Date().getMonth() you will see that it returns 8 instead of 9 (September), this is because in javascript months are zero-based-numbered

Categories

Resources