var date = new Date('2018-06-11'); // June 11
alert(date.getDay());
It returns 1 (correct) on desktop browsers (Firefox/Chrome), but it returns 0 (wrong) on mobile browsers (Android Chrome). Checked on multiple mobile phones, it returns 0.
I am still curious to know why the day of the week returns wrong on mobile browsers, but I am now using the following approach, which returns the correct value on mobile too, just in case anyone else stumbles into the same issue.
var date = '2018-06-11' // this date is input, and changes dynamically
var year = date.substring(0,4);
var month = (date.substring(5,7) < 10) ? date.substring(5,7).substring(1,2) - 1 : date.substring(5,7) - 1 // 1 is subtracted since JS has months as 0-11, not as 1-12. Why would anyone design it like this??
var day = date.substring(8,10)
var d = new Date();
d.setFullYear(year);
d.setMonth(month);
d.setDate(day);
alert(d.getDay()); // returns 1 (correct) both on desktop and mobile
The phones I tested, one in Europe, one in US, display the year, month and day correctly, but when I display the day of the week using date.getDay(), they did not display it correctly. It worked fine on desktop browsers, not on mobile browsers, that’s what I couldn’t understand. Timezone shouldn’t have an effect I believe, because the date is not read from the client locally.
The only thing that comes to my mind is that Android Chrome treated the date I enter 2018-06-11 (June 11), as November 6, which makes the wrong result correct. Is that possible? Isn’t the correct date format as YYYY-MM-DD in JS Date()? But that won’t work as an explanation because the phone shows correct date when I display the date in Y, M, D.
I will, for the moment, assume your timezone is somewhere West of GMT.
It depends. The browser may be interpreting “2018-06-11” as “2018-06-11 00:00:00 PST”, in which case the date would match correctly, and return the correct result.
If the browser instead has read “2018-06-11” as Zulu Time (2018-06-11 00:00:00 UTC), then the actual date value being stored is equivalent to 2018-06-10 17:00:00 PST, which would make the weekDay 0.
Try running new Date('2018-06-11').getTime() on both devices, and see if they spit out the same value.
EDIT: Here’s an example from my console, running Chrome on my laptop:
Keep in mind that new Date() sets the hour/minute/second to the Current time. If you’re doing straight date operations, that may not be an issue, but just something to keep in mind.
Well no, it’s not to be avoided per-se, it’s a global world view and storage you have to take into consideration.
You tell Javascript “2018-06-11”. Javascript interprets this as 2018-06-11 Zulu (UTC). That gets stored as a number. It is a moment in time - simultaneously around the world, at that moment, it is both Monday and Sunday - depending on which side of midnight you fall on. So in the US, at that moment, it is Weekday 0, and in Europe, it’s Weekday 1. It is locally many different hours of the day, in every timezone or DST-observation zone, etc. It is, however, EVERYWHERE, the exact same numerical value in timestamp form. Which is what Javascript (and pretty much every timezone-neutral timekeeper language in the world) stores.
Why? Because things happen at moments. Your meeting starts at 18:00, you say. Well no, it starts at 18:00 PDT on Monday. In England, it starts at 1 AM Tuesday. So is the meeting on a Monday or Tuesday? It depends. Computers don’t like it when questions have answers like “it depends”.
If you wanted to specify PDT, you could have said new Date('2018-06-11 00:00:00 PDT'), and Javascript would have interpreted it correctly. However, again, this is just a MOMENT. You haven’t fixed the problem, just shifted the line of measurement. (Somewhere in Eastern Asia, this moment is a Tuesday, not a Monday.)
Also, it’s not a question of browsers behaving differently - your desktop thinks its in Saudi Arabia (GMT+3), and your phone thinks it’s in California (GMT-7). Opposing sides of that midnight line.
I understand it better now. Thank you for the detailed explanation.
I now realized that for my specific need, which I need “Day 1” returned when I input “2018-06-11” regardless of the location in the world or how a browser treats it, I should use a string for my input, instead of a date object.