gpt4 book ai didi

javascript - "Object expected"尝试在 Javascript 上输入时间

转载 作者:行者123 更新时间:2023-12-02 14:49:35 24 4
gpt4 key购买 nike

错误是
线路:3
字符:3

var d = new Date();
var n = d.getHours();
var timeEnd = setHours(17);
var timeStart = setHours(15);

if(n < timeEnd && n > timeStart){
var r = confirm("Press a button");
if (r == true) {
x = "You pressed OK!";
_system.shutDown();
} else {
x = "You pressed Cancel!";
die;
}
}
else {
window.alert("This isn't the right time!")
}

如果有人可以帮助我解决该问题以及您看到的任何其他错误,那将会很有帮助。

最佳答案

您需要变量d来访问日期对象。

var timeEnd = d.setHours(17);
// ^^^
var timeStart = d.setHours(15);
// ^^^

我建议更改此行

var n = d.getHours();

var n = Date.now();

因为这适合用于比较的其他时间值。 Date#now :

The Date.now() method returns the number of milliseconds elapsed since 1 January 1970 00:00:00 UTC.

对应于 Date#setHours并使其具有可比性:

The setHours() method sets the hours for a specified date according to local time, and returns the number of milliseconds since 1 January 1970 00:00:00 UTC until the time represented by the updated Date instance.

关于javascript - "Object expected"尝试在 Javascript 上输入时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36292617/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com