Jes*_*sse 4 vbscript time operators
一周前我写了这个脚本,当你登录然后根据一天中的时间问候你时会启动它.今天早上它突然突然说:"无效时间"(如果所有其他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
Run Code Online (Sandbox Code Playgroud)
编辑:它似乎再次有效,现在已经是10点了但由于某种原因它在10之前没有用,我想我的代码中仍然有错误?
您正在比较时间和字符串数据子类型的值.的比较运算符(VBScript)的参考是一个关于它相当有点不清楚(或约自动数据亚型转换); 我猜测转换时间为字符串,并带有替代的前导零操作,例如#09:10:12#时间转换为字符串"9:10:12"或" 9:10:12"字符串.因此,使用时间文字强制时间比较,将它们包含在数字符号(#)中,例如#06:00:00#代替"06:00:00".
然而,仍然有你的逻辑差距:比如#06:00:00#或 #12:00:00#或#18:00:00#时间不匹配任何if也不elseif条件,将给予无效的时间输出.
因此,而不是
if ctime > "06:00:00" and ctime < "12:00:00" then
Run Code Online (Sandbox Code Playgroud)
使用其中之一
if ctime >= #06:00:00# and ctime < #12:00:00# then
Run Code Online (Sandbox Code Playgroud)
要么
if ctime > #06:00:00# and ctime <= #12:00:00# then
Run Code Online (Sandbox Code Playgroud)
并elseif类似地改进所有.
| 归档时间: |
|
| 查看次数: |
59 次 |
| 最近记录: |