I wrote a function several years ago that you might be able to modify to your needs. Expectation is that time is entered in h:m AM/PM format. Returns elapsed time in x Hrs y Mins. The second function converts this result into total minutes, if you need that. Hope this helps.
{! fn fnCalcElapsedTimeForSameDay_LC(Time1,Time2)
{
local s1,s2,h1,h2,m1,m2,i1,i2,t1,t2,eh,em,et,r
// if time is empty, do nothing
if Time1 == "" then
userok("No start time entered!")
return ""
endif
if Time2 == "" then
userok("No stop time entered!")
return ""
endif
// Break time down into its parts, based - 7 or 8 character string required
s1 = size(Time1)
if s1 == 7 then
h1 = sub(Time1,1,1)
m1 = sub(Time1,3,2)
i1 = sub(Time1,6,2)
else
if s1 == 8 then
h1 = sub(Time1,1,2)
m1 = sub(Time1,4,2)
i1 = sub(Time1,7,2)
else
userok("Invalid start time entered." + HRET + "Hour, Minute, and AM/PM are required.")
return ""
endif
endif
s2 = size(Time2)
if s2 == 7 then
h2 = sub(Time2,1,1)
m2 = sub(Time2,3,2)
i2 = sub(Time2,6,2)
else
if s2 == 8 then
h2 = sub(Time2,1,2)
m2 = sub(Time2,4,2)
i2 = sub(Time2,7,2)
else
userok("Invalid stop time entered." + HRET + "Hour, Minute, and AM/PM are required.")
return ""
endif
endif
// if AM/PM is PM, add 12 hours to convert to 24 hour clock
if i1 == "PM" and h1 < 12 then
h1 = val(h1) + 12
endif
if i2 == "PM" and h2 < 12 then
h2 = val(h2) + 12
endif
// convert minutes to hours
t1 = (h1 + (m1 / 60))
t2 = (h2 + (m2 / 60))
// do the math based on combination
cond
case i1 == "AM" and i2 == "AM" et = t2 - t1
case i1 == "AM" and i2 == "PM" et = t2 - t1
case i1 == "PM" and i2 == "AM" et = t1 - t2
case i1 == "PM" and i2 == "PM" et = t2 - t1
endcond
// get the hour, round the minutes, build the result
eh = truncate(et,0)
em = round((et - eh) * 60,0)
r = str(eh) + " hr " + str(em) + " min"
return str(r)
}
}
{! fn fnPushTime_TMV(strTime)
{
local retStr = ""
local strTemp = ""
local nSize
local strHrs = ""
local strMins = ""
local nHrsMins = ""
if strTime == "" then return "" else "" endif
strTemp = getfield(strTime," ","")
nSize = size(strTemp)
if nSize >= 1 then
strHrs = strTemp[1]
else
strHrs = ""
endif
if nSize >= 4 then
strMins = strTemp[4]
else
strMins = ""
endif
// userok("Size: " + str(nSize) + " : Hrs: " + strHrs + " : Mins: " + strMins)
if strHrs == "0" then
nHrsMins = 0
else
nHrsMins = val(strHrs) * 60
endif
retStr = str(nHrsMins + val(strMins))
return retStr
}
}
Posted : April 1, 2020 5:02 am