Tìm kiếm hỗ trợ

Tránh các lừa đảo về hỗ trợ. Chúng tôi sẽ không bao giờ yêu cầu bạn gọi hoặc nhắn tin đến số điện thoại hoặc chia sẻ thông tin cá nhân. Vui lòng báo cáo hoạt động đáng ngờ bằng cách sử dụng tùy chọn "Báo cáo lạm dụng".

Learn More

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

  • 4 trả lời
  • 1 gặp vấn đề này
  • 22 lượt xem
  • Trả lời mới nhất được viết bởi Wesley Branton

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.

Giải pháp được chọn

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.

Đọc câu trả lời này trong ngữ cảnh 👍 0

Tất cả các câu trả lời (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

Giải pháp được chọn

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.