Duration vbscript (vbs) function

依然范特西╮ 提交于 2020-02-03 01:43:26

问题


Is there a function to convert a specified number of seconds into a week/day/hour/minute/second time format in vbscript?

eg: 969234 seconds = 1wk 4days 5hrs 13mins 54secs


回答1:


Dim myDate
dim noWeeks
dim noDays
dim tempWeeks
dim pos 
myDate = DateAdd("s",969234,CDate(0))

tempWeeks = FormatNumber(myDate / 7,10)
pos = instr(tempWeeks, ".")
if pos > 1 then
    tempWeeks = left(myDate, pos -1)
end if
noWeeks = Cint(tempWeeks)

noDays = Cint(((myDate / 7) - noWeeks) * 7)

wscript.echo noWeeks & "wk " & noDays & "days " & datepart("h", myDate) & "hrs " & datepart("n", myDate) & "mins " & datepart("s", myDate) & "secs"



回答2:


No built in function to do that.

Here is a quick and dirty one:-

Function SecondsToString(totalSeconds)

    Dim work : work = totalSeconds

    Dim seconds
    Dim minutes
    Dim hours
    Dim days
    Dim weeks

    seconds = work Mod 60
    work = work \ 60
    minutes = work Mod 60
    work = work \ 60
    hours = work Mod 24
    work = work \ 24
    days = work Mod 7
    work = work \ 7
    weeks = work

    Dim s: s = ""
    Dim renderStarted: renderStarted = False

    If (weeks <> 0) Then
        renderStarted = True
        s = s & CStr(weeks)
        If (weeks = 1) Then
            s = s & "wk "
        Else
            s = s & "wks "
        End If
    End If

    If (days <> 0 OR renderStarted) Then
        renderStarted = True
        s = s & CStr(days)
        If (days = 1) Then
            s = s & "day "
        Else
            s = s & "days "
        End If
    End If

    If (hours <> 0 OR renderStarted) Then
        renderStarted = True
        s = s & CStr(hours)
        If (hours = 1) Then
            s = s & "hr "
        Else
            s = s & "hrs "
        End If
    End If

    If (minutes <> 0 OR renderStarted) Then
        renderStarted = True
        s = s & CStr(minutes)
        If (minutes = 1) Then
            s = s & "min "
        Else
            s = s & "mins "
        End If
    End If

    s = s & CStr(seconds)
    If (seconds = 1) Then
        s = s & "sec "
    Else
        s = s & "secs "
    End If

    SecondsToString = s

End Function


来源:https://stackoverflow.com/questions/797200/duration-vbscript-vbs-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!