Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
221 views
in Technique[技术] by (71.8m points)

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

question from:https://stackoverflow.com/questions/65892255/vbscript-can-we-find-out-duration-with-seconds-through-code

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...