X
點擊此處開啟此網站的行動版。

技術支援討論區

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

已張貼

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

<input onchange="wday(this.value);" type="text"> <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

額外的系統細節

已安裝的外掛程式

  • Citrix Receiver Plugin (Win32)
  • Citrix URL-Redirection Helper Plugin
  • Google Update
  • Intel web components updater - Installs and updates the Intel web components
  • Intel web components for Intel® Identity Protection Technology
  • NPRuntime Script Plug-in Library for Java(TM) Deploy
  • Next Generation Java Plug-in 11.101.2 for Mozilla browsers
  • The plug-in allows you to open and edit files using Microsoft Office applications
  • Office Authorization plug-in for NPAPI browsers
  • PDF-XChange Viewer Netscape Gecko Plugin
  • Shockwave Flash 22.0 r0
  • Skype Web Plugin 7.7.0.219
  • VLC media player Web Plugin

應用程式

  • User Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:48.0) Gecko/20100101 Firefox/48.0

更多資訊

Wesley Branton
  • Top 10 Contributor
582 個解決方法 4953 個答案

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.

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 [http://stackoverflow.com/ 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.

提出問題者

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.

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.

選擇的解決方法

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.

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.
Wesley Branton
  • Top 10 Contributor
582 個解決方法 4953 個答案

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.

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.