如何使用VBScript将数字转换为mm:ss格式

时间:2015-04-13 17:24:30

标签: time vbscript

我想使用VBScript以mm:ss格式转换数字(小于3600)。我面临的主要问题是添加前导零,以防我得到一个数字 例如:- 当尝试转换306 mm:ss格式时,我得到输出为5:6而不是05:06 这是我的代码..

entered_time = "306"
quotient = entered_time/60
quotient = Int(quotient)
MsgBox quotient
remainder = entered_time Mod 60
MsgBox remainder
time_format = quotient&":"&remainder
msgbox time_format

感谢您的帮助。

3 个答案:

答案 0 :(得分:2)

试试这样;)

intTotalSecs = 306
MsgBox intTotalSecs & "(s) ===> " & ConvertTime(intTotalSecs),vbinformation,"output time format as hh:mm:ss"
'************************************************************
Function ConvertTime(intTotalSecs)
Dim intHours,intMinutes,intSeconds,Time
intHours = intTotalSecs \ 3600
intMinutes = (intTotalSecs Mod 3600) \ 60
intSeconds = intTotalSecs Mod 60
ConvertTime = LPad(intHours) & " h : " & LPad(intMinutes) & " m : " & LPad(intSeconds) & " s"
End Function
'************************************************************
Function LPad(v) 
 LPad = Right("0" & v, 2) 
End Function
'************************************************************

答案 1 :(得分:0)

简单if remainder < 10怎么样?

答案 2 :(得分:0)

如果您的区域设置/区域设置合适,您只需:

>> setlocale "de-de"
>> secs = 308
>> ts = TimeSerial(0, 0, secs)
>> WScript.Echo FormatDateTime(ts, vbLongTime)
>>
00:05:08

如果不可能,请在基于.NET的格式类中加倍努力,如this one