Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

format("z") should be 0-based #34

Open
mzilverberg opened this issue Apr 29, 2018 · 6 comments
Open

format("z") should be 0-based #34

mzilverberg opened this issue Apr 29, 2018 · 6 comments

Comments

@mzilverberg
Copy link

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?

@elhidery
Copy link

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

@ghost
Copy link

ghost commented May 28, 2018

@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?

@elhidery
Copy link

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.

@ghost
Copy link

ghost commented May 28, 2018

@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.

@elhidery
Copy link

elhidery commented May 28, 2018

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.

@rejhgadellaa
Copy link

The documentation in the README should probably be updated, then? It says "0-365" ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants