1

I want to convert a number (less than 3600) in mm:ss format using VBScript. The main issue that i am facing is to add leading zeroes in case i get a single digit.
For example:- while trying to convert 306 in mm:ss format i get the output as 5:6 instead of 05:06.
This is my code..

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

Thanks for the help in advance.

user3331923
  • 39
  • 1
  • 4

3 Answers3

2

Try like this ;)

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
'************************************************************
Hackoo
  • 18,337
  • 3
  • 40
  • 70
0

What about a simple if remainder < 10 ?

Marcus Müller
  • 34,677
  • 4
  • 53
  • 94
0

If your locale/regional settings are suitable, you just do:

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

If that's not possible, put some effort in a .NET based format class like this one.

Community
  • 1
  • 1
Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96