我在VBScript中不擅长数学。
我正在努力解决这个问题。
我希望在脚本执行期间以分钟和秒计算时间。
stmr = Timer()
Wscript.Sleep 40000
m =FormatNumber(Timer() - stmr)/60, 0)
s = FormatNumber((Timer() - stmr), 0) - m*60
Wscript.Echo m & ":" &s
'=> 1m:-20s (wrong)
Wscript.Sleep 40000
m =(Timer() - stmr)/60
s = FormatNumber((Timer() - stmr), 0) - m*60
Wscript.Echo m & ":" &s
'=> 1m:20s (right)
还有更好的方法吗?
答案 0 :(得分:1)
试试这个vbscript:
StartTime = Timer 'Start the Timer
Pause 80 ' pause in seconds
MyTime = FormatNumber(Timer - StartTime, 0) ' in seconds
DurationTime = TimeSpan(MyTime)
wscript.echo DurationTime
'*******************************
Function TimeSpan(MyTime)
seconds = FormatNumber(MyTime, 0)
minutes = seconds \ 60
hours = minutes \ 60
minutes = minutes mod 60
seconds = seconds mod 60
hours = hours mod 24
if len(hours) = 1 then hours = "0" & hours
TimeSpan = RIGHT("00" & hours , 2) & ":" & _
RIGHT("00" & minutes, 2) & ":" & _
RIGHT("00" & seconds, 2)
End Function
'*******************************
Sub Pause(NSeconds)
Wscript.Sleep(NSeconds*1000)
End Sub
'*******************************
答案 1 :(得分:1)
使用文档中描述的Timer()
非常简单;
返回自12:00 AM(午夜)起经过的秒数。
以最简单的形式检查脚本的已用时间,您需要先阅读Timer()
的开头和结尾,然后查看差异。
Dim st, et
'Start Time
st = Timer()
'Long running task
WScript.Sleep 10000
'End Time
et = Timer()
'Display the difference
WScript.Echo "Time Elapsed (sec) :" & et - st
输出:
Time Elapsed (sec) :10.00391
一旦你在几秒钟内得到了原始结果,你可以做任何你喜欢的事情,把它转换成分钟和秒,把它格式化为字符串做你想做的任何事情。请记住,FormatNumber()
,FormatDateTime()
等函数旨在用于显示输出,并且在执行计算时不应使用。
遵循from the comment类@Hackoo's is acceptable之类的方法,但也可以在不需要FormatNumber()
的情况下完成。
Option Explicit
Dim st, i
st = Timer()
For i = 1 to 10
WScript.Sleep 1000
WScript.Echo FormatTime(Timer() - st)
Next
Function FormatTime(secs)
Dim t, a
secs = Int(secs)
a = Array(CStr(Right("00" & Int(secs / 3600) Mod 24, 2)), CStr(Right("00" & Int(secs / 60) Mod 60, 2)), CStr(Right("00" & secs Mod 60, 2)))
FormatTime = Join(a, ":")
End Function
使用Mod()
时,您传递的任何值都是整数,或者您会得到奇怪的结果(因此您在示例中使用了Int()
) < / em>的
输出:
00:00:01
00:00:02
00:00:03
00:00:04
00:00:05
00:00:06
00:00:07
00:00:08
00:00:09
00:00:10
答案 2 :(得分:0)
我有更好的方法。
stmr = Now()
for i = 0 to 100
Wscript.Echo (split(FormatDateTime((Now() - stmr), 3), ":")(0))-12 & "h : " _
& split(FormatDateTime((Now() - stmr), 3), ":")(1) & "m : " _
& split(split(FormatDateTime((Now() - stmr), 3), ":")(2))(0) & "s"
Wscript.Sleep 1000
i = i + 1
next