I'm looking for VFE programming to add a button which calculates estimated gestational age in the format of 39 weeks and 6 days.
Does anybody have somethingi like this in any of their OB forms?
Assuming you know the estimated conception date and will pass it when you call the function, something like this should work. I wrote this really quickly and haven't tested this, but here is how it works:
1. Determine the age in days by comparing today's date with the conception date
2. If this age in days is less than 7, return a value that just has number of days
3. If this age in days is more than 7, use truncate to get a rounded down number of weeks. Once you get this number, multiply number of weeks by 7 and compare it to number of days. This should give you the second part of the statement "x days"
Hopefully this (or something similar) works for you.
fn calculateGestationalAge(conceptionDate){
local retStr = ""
local weeks = 0
local ageDays = DURATIONDAYS(conceptionDate, str(._TODAYSDATE))
cond
case ageDays < 7: retStr = str(ageDays) + " days"
else:
weeks = truncate((ageDays/7),0)
retStr = weeks + " weeks and " + str(ageDays - (weeks * 7)) + " days"
endcond
return retStr
}
Here's what we have been using for years. It uses the "EDC" obs term if there is one (this is where we store EDD by ultrasound). If not, then it uses LMP. It also populates the "GEST AGE BY" obs term with text indicating which method was used.
/*calculate EGA*/
{!
OBSNOW("GEST AGE BY","No dates found") //set this as default
OBSNOW("GEST AGE","") //blank at start
if (OBSANY("LAST MP")"") then
OBSNOW("GEST AGE BY","GA calculated from LMP")
local lDays = val(DURATIONDAYS(OBSANY("LAST MP"), str(DOCUMENT.CLINICALDATE)))
OBSNOW("GEST AGE",(div(lDays, 7) + "W" + mod(lDays, 7) + "D"))
endif
if (OBSANY("EDC")"") then
daysOld=DURATIONDAYS(OBSANY("EDC"),str(._TODAYSDATE))
if(daysOld>20) then
OBSNOW("GEST AGE BY","LMP")
local lDays = val(DURATIONDAYS(OBSANY("LAST MP"), str(DOCUMENT.CLINICALDATE)))
OBSNOW("GEST AGE",(div(lDays, 7) + "W" + mod(lDays, 7) + "D"))
else
startDate=SUBTRACTDATES(OBSANY("EDC"), "0", "0", "280")
OBSNOW("GEST AGE BY","Final EDD")
local lDays = val(DURATIONDAYS(startDate, str(DOCUMENT.CLINICALDATE)))
OBSNOW("GEST AGE",(div(lDays, 7) + "W" + mod(lDays, 7) + "D"))
endif
endif
}