0

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.

  • Does this answer your question? [Format current date and time in VBScript](https://stackoverflow.com/questions/22574092/format-current-date-and-time-in-vbscript) – user692942 May 15 '21 at 07:56
  • Use `Minute()` and `Second()` to get the corresponding numeric values, then convert the numeric minutes to seconds by multiplying by `60` then add them to the seconds. – user692942 May 15 '21 at 10:05

2 Answers2

0

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
'-----------------------------------------------------------------------------
Hackoo
  • 18,337
  • 3
  • 40
  • 70
  • Suggested how to do it, plenty of existing questions cover date conversion, what does writing it for them accomplish? Plus it doesn’t need to be any where near that complicated. – user692942 May 15 '21 at 14:19
  • Also, why not use the built-in date time functions instead if relying on splitting string values? You’re not even converting the strings to integers before calculating them. – user692942 May 15 '21 at 14:26
0

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.


Useful Links

user692942
  • 16,398
  • 7
  • 76
  • 175