接线员突然不工作
Operators suddenly not working
一周前我写了这个脚本,它在您登录时启动,然后根据一天中的时间向您致意。今天早上它突然突然说:"Invalid time"(如果所有其他 elseif
选项都不匹配时间,它会这样做)。这以前一直有效到今天。
这是我的代码:
Set objShell = CreateObject("WScript.Shell")
ctime = Time()
usr = objShell.ExpandEnvironmentStrings("%username%")
if ctime > "06:00:00" and ctime < "12:00:00" then
objShell.Popup "Good Morning, " & usr, 5, "", 4096
elseif ctime > "12:00:00" and ctime < "18:00:00" then
objShell.Popup "Good Afternoon, " & usr, 5, "", 4096
elseif ctime > "18:00:00" and ctime < "23:59:59" then
objShell.Popup "Good evening, " & usr, 5, "", 4096
elseif ctime > "00:00:00" and ctime < "06:00:00" then
objShell.Popup "Good night, " & usr, 5, "", 4096
else
objShell.Popup "Invalid time", 5, "", 4096
end if
编辑:它似乎又可以工作了,现在是 10 点钟,但由于某种原因它在 10 点之前没有工作,我想我的代码中仍然有错误吗?
您正在比较 time 和 string 数据子类型的值。 Comparison Operators (VBScript) reference 对此(或关于自动数据子类型转换)有点不清楚;我想转换 time 到 string 有一个替代的前导零操作,例如 #09:10:12#
时间转换为 "9:10:12"
或" 9:10:12"
字符串。因此,通过将时间文字括在数字符号 (#
) 中强制使用时间文字进行 time 比较,例如 #06:00:00#
而不是 "06:00:00"
.
但是,您的逻辑仍然存在漏洞:例如 #06:00:00#
or #12:00:00#
or #18:00:00#
time does not match any if
nor elseif
条件并将给出 Invalid time 输出。
因此,而不是
if ctime > "06:00:00" and ctime < "12:00:00" then
使用其中之一
if ctime >= #06:00:00# and ctime < #12:00:00# then
或
if ctime > #06:00:00# and ctime <= #12:00:00# then
并类似地改进所有 elseif
。
一周前我写了这个脚本,它在您登录时启动,然后根据一天中的时间向您致意。今天早上它突然突然说:"Invalid time"(如果所有其他 elseif
选项都不匹配时间,它会这样做)。这以前一直有效到今天。
这是我的代码:
Set objShell = CreateObject("WScript.Shell")
ctime = Time()
usr = objShell.ExpandEnvironmentStrings("%username%")
if ctime > "06:00:00" and ctime < "12:00:00" then
objShell.Popup "Good Morning, " & usr, 5, "", 4096
elseif ctime > "12:00:00" and ctime < "18:00:00" then
objShell.Popup "Good Afternoon, " & usr, 5, "", 4096
elseif ctime > "18:00:00" and ctime < "23:59:59" then
objShell.Popup "Good evening, " & usr, 5, "", 4096
elseif ctime > "00:00:00" and ctime < "06:00:00" then
objShell.Popup "Good night, " & usr, 5, "", 4096
else
objShell.Popup "Invalid time", 5, "", 4096
end if
编辑:它似乎又可以工作了,现在是 10 点钟,但由于某种原因它在 10 点之前没有工作,我想我的代码中仍然有错误吗?
您正在比较 time 和 string 数据子类型的值。 Comparison Operators (VBScript) reference 对此(或关于自动数据子类型转换)有点不清楚;我想转换 time 到 string 有一个替代的前导零操作,例如 #09:10:12#
时间转换为 "9:10:12"
或" 9:10:12"
字符串。因此,通过将时间文字括在数字符号 (#
) 中强制使用时间文字进行 time 比较,例如 #06:00:00#
而不是 "06:00:00"
.
但是,您的逻辑仍然存在漏洞:例如 #06:00:00#
or #12:00:00#
or #18:00:00#
time does not match any if
nor elseif
条件并将给出 Invalid time 输出。
因此,而不是
if ctime > "06:00:00" and ctime < "12:00:00" then
使用其中之一
if ctime >= #06:00:00# and ctime < #12:00:00# then
或
if ctime > #06:00:00# and ctime <= #12:00:00# then
并类似地改进所有 elseif
。