Hi all,
I have a fn allowing me to subtract end time from start time in VFE. The problem is with the noon hour. When client sees a patient from 11:45 to 12:15 for example, the calculated visit time comes out to be 765 minutes because the fn is set to calculate from midnight. It works great for all AM times and all PM times but cannot differentiate noon from midnight. I think I need a conversion to military time? And I'm in over my head here. Thank you for any help working this out..........fn below:
{DOCUMENT.RESULT=fnCareManagementCalculateHoursAndMinutes(DOCUMENT.START_DATE_TIME,DOCUMENT.END_DATE_TIME)}
{
fn fnCareManagementCalculateHoursAndMinutes(start,end)
{
if ok(start) and start<>"" and ok(end) and end<>"" then
//Make sure dates are the same
if durationdays(start,end)<>0 then
userok("Dates should be the same")
return ""
endif
//Parse first time-date stamp
local startMinutes=fnCareManagementParseHoursMinutes(start)
//Parse second time-date stamp
local endMinutes=fnCareManagementParseHoursMinutes(end)
//return difference
endMinutes-startMinutes
endif
}
}
{fn fnCareManagementParseHoursMinutes(dateTime)
{
local i,n=size(dateTime)
//Get AM/PM indicator from final 2 chars:
local AmPmIndicator=sub(dateTime,n-1,2)
//Now scan for the colon that separates hours and minutes
local idx=match(dateTime,":")
//Scan minutes:
local temp,tempMins="",tempHrs=""
for i=idx+1,i<=n,i=i+1
do
temp=sub(dateTime,i,1)
if temp==" " then
break
endif
tempMins=tempMins+temp
endfor
//Scan hours:
for i=idx-1,i>0,i=i-1
do
temp=sub(dateTime,i,1)
if temp==" " then
break
endif
tempHrs=temp+tempHrs
endfor
//Combine hours and minutes to get total minutes
local totalMinutesFromMidnight=(tempHrs*60)+tempMins
if AmPmIndicator="PM" then
totalMinutesFromMidnight=totalMinutesFromMidnight+720
endif
return totalMinutesFromMidnight
}
}
/**this one below does not calculate at all
{
fn fnCareManagementParseHoursMinutes(dateTime)
{
local i,minutes,n=size(dateTime)
//Get AM/PM indicator from final 2 chars:
local AmPmIndicator=sub(dateTime,n-1,2)
if AmPmIndicator="PM" then
minutes=720
endif
//Now scan for the colon that separates hours and minutes
local idx=match(dateTime,":")
//Scan minutes:
local temp,tempMins="",tempHrs=""
for i=idx+1,i<=n,i=i+1
do
temp=sub(dateTime,i,1)
if temp==" " then
break
endif
tempMins=tempMins+temp
endfor
//Scan hours:
for i=idx+1-1,i>0,i=i-1
do
temp=sub(dateTime,i,1)
if temp==" " then
break
endif
tempHrs=temp+tempHrs
endfor
//Combine hours and minutes to get total minutes
minutes=minutes+(tempHrs*60)+tempMins
return minutes
}
**/
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
}
}
Thank you Lee, big help!! Have a great day!
Debby
Here's a test form that has an elapsed time. I think it takes care of midnight.
Thank you David!
Debby