format("z") should be 0-based
PHP uses zero-based numbering for "z" in date formatting and returns values from 0 - 365. This library returns values from 1 - 366. In my opinion, to fully mimic PHP, the function should end with a subtraction of 1:
(linebreaks added for readability)
z: function () {
var d = new Date(this.getFullYear(), 0, 1);
return Math.ceil((this - d) / 86400000) - 1
},
Or was there a motivation to not used zero-based numbering here?
Hello,
If you to be zero based you can use Math.floor instead of math.ceil and remove -1 :)
var now = new Date(); var start = new Date(now.getFullYear(), 0, 1); var d = new Date(now.getFullYear(), 0, 1); alert(Math.floor((now - d) / 86400000));
I hope that will resolve your issue .
Br, Elhidery
@elhidery unless I'm mistaken the question not about how to convert the function to be zero based (which is trivial indeed ;-). It is about (quoting the question): was there a motivation to not used zero-based numbering here?
Hello again,
I apologize for or misunderstanding. This below line of code var d = new Date(this.getFullYear(), 0, 1);
This.getFullYearr returns the current year which is "2018" the "0" refers to january because the months are zero based ,finally the day is stored as 1 because the domain in javascript is formatted from 1 to 31 ,if the day is written as "0" it will translate to the final day of the corresponding month.
So the modulation to not use zero based numbering is to avoid conflicts with the javascript standards in which the value of "0" for the day corresponds to the maximum day value of the corresponding month.
@elhidery unless I'm mistaken the question is not about the rationale for how zero is used in the Date function argument. It is about the range of values returned by the z function copy/pasted in the message.
I ran a few test case scenarios and concluded that it is due to how JS subtracts.
The test scenario that made this obvious was the following: var d1 = new Date(2018, 0, 1); Which corresponds to 1/Jan/2018. var d2 = new Date(2017, 0, 0); Which corresponds to 31/Jan/2017
The expected result should have been 335, yet the output was 366, which exceeds the range of the output and this occurs due to the way JS handles the subtraction. When the subtraction occurs JS deals with 0 and 1 as numbers. It does not reference the 0 to the max day value for the corresponding month as it should according to the dating standard. This results in an erroneous values if we made the date 0 based.
The documentation in the README should probably be updated, then? It says "0-365" ;)