Search code examples
vbscript

VBScript - Can we find out duration with seconds through code


I have time in seconds... Like below

set seconds = 180 seconds
set seconds1 = 1500 Seconds
set seconds2 = 600 Seconds

based on above values output like below

duration = 00:03:00 
duration = 00:25:00
duration = 00:10:00

like i need through VBscript pls help me


Solution

  • You can manually build your HH:MM:SS string by dividing the total number of seconds into hours and minutes:

    Function FormatSeconds(p_lngSeconds)
    
        Dim sReturn
        Dim lngTotalSeconds
        Dim iHours
        Dim iMinutes
        
        ' Copy value from parameter
        lngTotalSeconds = p_lngSeconds
        
        ' Calculate number of hours
        iHours = Int(lngTotalSeconds / (60 * 60))
        
        ' Subtract from total seconds
        lngTotalSeconds = lngTotalSeconds - (iHours * 60 * 60)
        
        ' Calculate numer of minutes
        iMinutes = Int(lngTotalSeconds / 60)
        
        ' Subtract from total seconds
        lngTotalSeconds = lngTotalSeconds - (iMinutes * 60)
        
        ' Build string
        sReturn = iHours & ":" & Right("0" & iMinutes, 2) & ":" & Right("0" & lngTotalSeconds, 2)
    
        FormatSeconds = sReturn
    
    End Function
    

    Going back to your question, you'd use this function thusly:

    duration = FormatSeconds(seconds)