- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I found a very interesting JavaScript Date() wrong result when using XDK.
Setting a Date in Date(year,month,day) format will add one more month on result.
I am not sure it is only on XDK or Javascript itself. (It seems from the Javascript itself.)
The test code is as below for your test.
<!DOCTYPE html> <html> <body> <p id="demo"></p> <script> var d1 = new Date("2016/01/01"); var d2 = new Date(2016,01,01); document.getElementById("demo").innerHTML = "Date('2016/01/01') = "+d1 +"<br>It shows the right date.<br>" +"<br>Date(2016,1,1) = "+d2+"<br>It will add one month to show the wrong date."; </script> </body> </html>
/* You will get result as below ---------------------------------------------------------------
Date('2016/01/01')=Fri Jan 01 2016 00:00:00 GMT+0800 (CST)
It shows the right date.
Date(2016,1,1)=Mon Feb 01 2016 00:00:00 GMT+0800 (CST)
It will add one month to show the wrong date.
-----------------------------------------------------------------------------------------------------*/
- Tags:
- HTML5
- Intel® XDK
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Its likely because of the following:
"2016/01/01" is already a "real date", so javascript wil (try to) convert the string date to a date object.
Date(2016,1,1) wil make you a new date, with month zero based: 0 =Jan, 1 = Feb, 2 = March, 3 = April etc..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Dear Wesley,
Thank for your reply. You may be right in technically.
But Month should start from one, right?
It is wrong or right action of Javascript on XDK?
Should Javascript or XDK need to improve it?
Matrix
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi Matrix,
It is the way the Javascript library is. In Javascript programming (and many other languages), array references used 0 based counting. So 0 is the index of the first item, etc. As a side effect of that, the Date library is functioning as you discover. Weirdly, days are numbered "normally".
It is not a bug in the XDK or Javascript. It is merely a questionable design decision that was made a long time ago.
http://stackoverflow.com/questions/1453043/zero-based-month-numbering
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
When you titled this forum thread you named it "JavaScript Date() wrong result when using XDK."
But did you actually compare it to any other Javascript installations, like in Chrome or Firefox or Node?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Dear Chris,
Yes, just trying on Chrome, Firefox and Safiri.
There are all have the same problem.
Just as your saying, it is not consistency in the rules of month and day.
May be it is time to suggest Javascript to refine.
And Date() is not convenient to do compare operation like DateA>DateB, may be XDK can build a JS Dates library to improve all these problems.
The Date() comparison method could like below for reference.
// Source: http://stackoverflow.com/questions/497790 var dates = { convert:function(d) { // Converts the date in d to a date-object. The input can be: // a date object: returned without modification // an array : Interpreted as [year,month,day]. NOTE: month is 0-11. // a number : Interpreted as number of milliseconds // since 1 Jan 1970 (a timestamp) // a string : Any format supported by the javascript engine, like // "YYYY/MM/DD", "MM/DD/YYYY", "Jan 31 2009" etc. // an object : Interpreted as an object with year, month and date // attributes. **NOTE** month is 0-11. return ( d.constructor === Date ? d : d.constructor === Array ? new Date(d[0],d[1],d[2]) : d.constructor === Number ? new Date(d) : d.constructor === String ? new Date(d) : typeof d === "object" ? new Date(d.year,d.month,d.date) : NaN ); }, compare:function(a,b) { // Compare two dates (could be of any type supported by the convert // function above) and returns: // -1 : if a < b // 0 : if a = b // 1 : if a > b // NaN : if a or b is an illegal date // NOTE: The code inside isFinite does an assignment (=). return ( isFinite(a=this.convert(a).valueOf()) && isFinite(b=this.convert(b).valueOf()) ? (a>b)-(a<b) : NaN ); }, inRange:function(d,start,end) { // Checks if date in d is between dates in start and end. // Returns a boolean or NaN: // true : if d is between start and end (inclusive) // false : if d is before start or after end // NaN : if one or more of the dates is illegal. // NOTE: The code inside isFinite does an assignment (=). return ( isFinite(d=this.convert(d).valueOf()) && isFinite(start=this.convert(start).valueOf()) && isFinite(end=this.convert(end).valueOf()) ? start <= d && d <= end : NaN ); } }

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page