Reputation: 15039
Hi I have a report that uses vbscript
to do my logic with my fields.
The report has a field that are the total minutes and has big values such: 1950 minutes, etc.
I need to update other field in order to convert those minutes to a readable format like 4 days 2 hours 30 minutes. My days are for 8 hours because is my labor day.
So 16 hours = 2 days and so on.
Right now my logic is this one:
' 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
But I'm not getting the correct result, any clue?
Upvotes: 1
Views: 2301
Reputation: 38745
Compute the larger units by integer division (), substract the minutes 'used' by the larger units from your input value. In code:
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
output:
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
Upvotes: 2