0

Javascript weekday wrong result with Date.getUTCDay(), why?

Hello everybody, as the topic title already said: Why do I get a wrong result with the getUTCDay() method of the Date Object? See code output for details. Many thanks in advance https://code.sololearn.com/WE2UpHFjA2rM/?ref=app

5th Mar 2019, 6:55 PM
Pete Wright
Pete Wright - avatar
10 odpowiedzi
+ 1
Pete Wright , sorry but I don't understand your point. It's necessary to add 1 in the first case, because you rearrange the order of the days of the week. The "original value" of the method getUTCDay() in the first case is always displaced with one day. Hope now it is clear for you.
5th Mar 2019, 8:13 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 1
It's really strange. Should be checked deeply.
5th Mar 2019, 9:41 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 1
I think I figured it out, but I'd like someone to confirm that. The following should create a new Date (Sunday, 31. of March 2019) > var d1 = new Date(2019, 2, 31); But different browsers return different values. On the latest Mozilla Firefox it returns: > Date 2019-03-30T23:00:00.000Z (which is the UTC time) While the latest Opera Stable (which is my primary browser) returns this: > Sun Mar 31 2019 00:00:00 GMT+0100 (Central European Standard Time) Keeping this Offset in mind, I did the following, to make sure that the right day will be returned in all major browsers: > var d2 = new Date (2019, 2, 31, 12, 0, 0) This will also set the time to 12:00 (PM) which is the middle of the day (and hopefully the right date) Now I have some questions: 1. Why do the browsers return different dates and is there anything that can be done to changes this behavior? 2. Is the method listed last (date + time) safe to use in my codes? 3. Will it return the exact same output/date on ALL major browsers? Many thanks in advance
30th Mar 2019, 11:51 PM
Pete Wright
Pete Wright - avatar
0
I think I found the error. In line 18 it should be ww[w1] https://code.sololearn.com/WQY7c78bdVK5/?ref=app Hope it helps you.
5th Mar 2019, 7:48 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
0
TheWhiteCat💡 Thanks for your effort. I think that is not what I was looking for. Since w1 = w0 + 1 what means that the actual return value of the getUTCDay() method is being modified! But the result should be correct with the original return value! Any ideas?
5th Mar 2019, 7:57 PM
Pete Wright
Pete Wright - avatar
0
TheWhiteCat💡 The original getUTCDay() will return 0 for "Sunday", 1 for "Monday", 2 for "Tuesday", etc. Right??? So for the Date "01.01.1970" it should return 4, since the 1st of January 1970 was a Thursday. Instead, the original method returns 3!!! Check out my code and press the "weekday 0-6" button. It returns the UNMODIFIED ORIGINAL VALUE of getUTCDay(). But instead of giving me a "4" it just returns "3" That's what I don't understand. The other buttons, return values and variables are not important.
5th Mar 2019, 8:25 PM
Pete Wright
Pete Wright - avatar
0
Now I understand your point. I think I corrected it. Please see the code https://code.sololearn.com/WQY7c78bdVK5/?ref=app
5th Mar 2019, 8:38 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
0
TheWhiteCat💡 eeeerm... what exactly did you change? Could you please comment the changes?
5th Mar 2019, 8:41 PM
Pete Wright
Pete Wright - avatar
0
In lines 39=> fx = w1 and 42 => fx = w0 of the switch-case construction.
5th Mar 2019, 8:44 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
0
TheWhiteCat💡 I see But that's still wrong, because now the "weekday 1-7" button returns 3, what would be the 3rd day => "Tuesday" (when starting with Sunday) 😭😭😭
5th Mar 2019, 8:48 PM
Pete Wright
Pete Wright - avatar