搜尋 Mozilla 技術支援網站

防止技術支援詐騙。我們絕對不會要求您撥打電話或發送簡訊,或是提供個人資訊。請用「回報濫用」功能回報可疑的行為。

Learn More

javascript date-function returns wrong weekday for Sep 1,2017 and later when setDate is used AFTER setMonth and/or setFullYear

more options

FF 48.0.2 Sample Code: Try to enter 1.9.17 which is a friday

<input type="text" onchange="wday(this.value);"> <script> function wday() { var x = wday.arguments[0].split("."); if (x[2].length<4) x[2] += 2000; var y = new Date(); y.setFullYear(x[2]); y.setMonth(x[1]-1); y.setDate(x[0]); var z = new Array("So","Mo","Di","Mi","Do","Fr","Sa"); alert("wrong "+z[y.getDay()]) wday2(wday.arguments) } function wday2() { var x = wday.arguments[0].split("."); if (x[2].length<4) x[2] += 2000; var y = new Date(); y.setDate(x[0]); y.setMonth(x[1]-1); y.setFullYear(x[2]); var z = new Array("So","Mo","Di","Mi","Do","Fr","Sa"); alert("correct "+z[y.getDay()]) } </script>

BTW: Same error with Chrome and Edge.

FF 48.0.2 Sample Code: Try to enter 1.9.17 which is a friday <html> <head></head> <body> <input type="text" onchange="wday(this.value);"> <script> function wday() { var x = wday.arguments[0].split("."); if (x[2].length<4) x[2] += 2000; var y = new Date(); y.setFullYear(x[2]); y.setMonth(x[1]-1); y.setDate(x[0]); var z = new Array("So","Mo","Di","Mi","Do","Fr","Sa"); alert("wrong "+z[y.getDay()]) wday2(wday.arguments) } function wday2() { var x = wday.arguments[0].split("."); if (x[2].length<4) x[2] += 2000; var y = new Date(); y.setDate(x[0]); y.setMonth(x[1]-1); y.setFullYear(x[2]); var z = new Array("So","Mo","Di","Mi","Do","Fr","Sa"); alert("correct "+z[y.getDay()]) } </script> </body> </html> BTW: Same error with Chrome and Edge.

被選擇的解決方法

Reply from StackOverflow: new Date() has the current date as initial value (I was not aware of that fact). That messes up my settings. Setting the day first avoids the problem. My fault. Thanks for the hint.

從原來的回覆中察看解決方案 👍 0

所有回覆 (4)

more options

Since the issue occurs on all browsers, it is likely an issue with your code, not the browser. This forum is used for Firefox specific issues.

Since your issue is not Firefox specific, you would probably have better luck posting your question to Stack Overflow. There are plenty of people that help to fix other peoples' codes, so you should have no trouble getting the help that you need.

Sorry for any inconvenience.

more options

Thank you for the quick response. In fact my favourite browser is FF and when I checked the others I was puzzled about getting the same issue...

I can not imagine that the code is wrong since changing the sequence of 1 statement and returns correct results. And it occurs only for dates Sep 1, 2017 and later. Could be an issue in the javascript engine...

Anyway: I'll post it on StackOverflow.

more options

選擇的解決方法

Reply from StackOverflow: new Date() has the current date as initial value (I was not aware of that fact). That messes up my settings. Setting the day first avoids the problem. My fault. Thanks for the hint.

more options

No problem. I find that there are tons of great and friendly people on Stack Overflow that are more than willing to help you solve your programming issues.

If you do have any issues with Firefox in the future, feel free to get in touch with the Mozilla support team again. Thanks.