vbscript如何将分钟转换为日,小时和分钟格式

时间:2015-03-01 20:48:58

标签: vbscript

您好我有一份报告,使用vbscript对我的字段执行逻辑。

报告的字段是总分钟数,具有较大的值:1950分钟等等。

我需要更新其他字段才能将这些分钟转换为可读格式,例如4天2小时30分钟。我的日子是8个小时,因为是我的劳动节。

所以16小时= 2天等等。

现在我的逻辑就是这个:

 ' get the total minutes
total = sum(lateminutes)

' divide the minutes by 60 to get hours
hours = Int(total) / 60 

' get the division difference and convert it to minutes
minutes = Round((hours - Int(hours)) * 60)

' convert my hours to days by dividing into 8 (8 hours per day)
days = Int(hours) / 8 

if Int(days) > 0 then
text = Cstr(Int(days)) & " day(s)" & " "
end if

if Int(hours) > 0 then
text = text & Cstr(Int(hours)) & " hours" & " "
endif

if Int(minutes) > 0 then
text = text & minutes & " min"
endif

testfield.Text = text 
  

但是我没有得到正确的结果,任何线索?

1 个答案:

答案 0 :(得分:2)

通过整数除法()计算较大的单位,减去分钟数并使用'来自您输入值的较大单位。在代码中:

Option Explicit

Const cnMWD = 480 ' 8 * 60 mins in 8 hour working day
Const cnMWH =  60 ' 60 mins in 60 min working hour

Dim otp : otp = Array(0, "day(s)", 0, "hour(s)", 0, "mins")
Dim m, mm
For Each m In Array(59, 61, 479, 480, 485, 545, 2060)
    mm     = m
    otp(0) = m \ cnMWD
    m      = m - otp(0) * cnMWD
    otp(2) = m \ cnMWH
    m      = m - otp(2) * cnMWH
    otp(4) = m
    WScript.Echo mm, "=>", Join(otp)
Next

输出:

cscript 28798880.vbs
59 => 0 day(s) 0 hour(s) 59 mins
61 => 0 day(s) 1 hour(s) 1 mins
479 => 0 day(s) 7 hour(s) 59 mins
480 => 1 day(s) 0 hour(s) 0 mins
485 => 1 day(s) 0 hour(s) 5 mins
545 => 1 day(s) 1 hour(s) 5 mins
2060 => 4 day(s) 2 hour(s) 20 mins