I want to convert normal time format eg 00:00:00 OR HH:MM:SS to seconds only like 1 minute and 20 seconds make 80 seconds by using VBScript.
And also if anyone can help me to make a time format input for VBScript like 00:00:00 OR HH:MM:SS.
I want to convert normal time format eg 00:00:00 OR HH:MM:SS to seconds only like 1 minute and 20 seconds make 80 seconds by using VBScript.
And also if anyone can help me to make a time format input for VBScript like 00:00:00 OR HH:MM:SS.
Perhaps something like that :
Title = "Converting Time Format [HH:mm:ss] to seconds"
Do
strInput = InputBox("Enter format time like HH:mm:ss",Title,"00:01:20")
Loop Until IsValid_Format_Time(strInput) = True
Msgbox strInput & " = "& Convert_Time_Seconds(strInput) & " seconds",vbInformation,Title
MyTime = Convert_Time_Seconds(strInput)
MsgBox MyTime & " = " & Convert_Min_Sec(MyTime),vbInformation,Title
'-----------------------------------------------------------------------------
Function Convert_Min_Sec(MyTime)
Const SECONDS_IN_DAY = 86400
Const SECONDS_IN_HOUR = 3600
Const SECONDS_IN_MINUTE = 60
seconds = Round(MyTime, 2)
If seconds < SECONDS_IN_MINUTE Then
Convert_Min_Sec = seconds & " seconds "
Exit Function
End If
If seconds < SECONDS_IN_HOUR Then
minutes = seconds / SECONDS_IN_MINUTE
seconds = seconds MOD SECONDS_IN_MINUTE
Convert_Min_Sec = Int(minutes) & " minutes " & seconds & " seconds "
Exit Function
End If
If seconds < SECONDS_IN_DAY Then
hours = seconds / SECONDS_IN_HOUR
minutes = (seconds MOD SECONDS_IN_HOUR) / SECONDS_IN_MINUTE
seconds = (seconds MOD SECONDS_IN_HOUR) MOD SECONDS_IN_MINUTE
Convert_Min_Sec = Int(hours) & " hours " & Int(minutes) & " minutes " & seconds & " seconds "
Exit Function
End If
End Function
'-----------------------------------------------------------------------------
Function Convert_Time_Seconds(Time)
actualTime = Split(time,":")
TotalSeconds = (Int(actualTime(0))) * 60 * 60 + (Int(actualTime(1))) * 60 + (Int(actualTime(2)))
Convert_Time_Seconds=TotalSeconds
End Function
'-----------------------------------------------------------------------------
Function IsValid_Format_Time(MyTime)
Set RegEx= New RegExp
With RegEx
.Pattern = "^([0-1]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$"
If .Test(MyTime)= True then
IsValid_Format_Time = True
end if
End With
End Function
'-----------------------------------------------------------------------------
The best approach to accomplish this is using the in-built date-time functions available in VBScript. The key is to use the Hour()
, Minute()
and Second()
functions to convert the Date
subtype value to an integer representing the various units (hours, minutes and seconds). Once we have this converting them all to a common value (like seconds) just requires a little basic maths.
Option Explicit
Function ConvertTimeToSeconds(value)
Dim dateTime: dateTime = TimeValue(value)
Dim numOfHours: numOfHours = Hour(dateTime)
Dim numOfMinutes: numOfMinutes = Minute(dateTime)
Dim numOfSeconds: numOfSeconds = Second(dateTime)
ConvertTimeToSeconds = (((numOfHours * 60) * 60) + (numOfMinutes * 60) + numOfSeconds)
End Function
Call WScript.Echo("Number of Seconds = " & ConvertTimeToSeconds("00:01:20"))
Output:
Number of Seconds = 80
Note: Try to avoid using string manipulation when working with dates as results can be unpredictable and will be affected by changes to system regional settings. The only time string manipulation should be used is when outputting the final formatted result.