Calculate Gestational Age based on months and weeks

D

Dr. Momo

Hi, SO I want to make a pregnancy wheel:
(LMP(Date) - 3months, +7d)= Estimated Date of Delivery (it also has to
advance to the next year if >April.
Also, I want to be able to automaticallt update the current Gestational
age(#weeks/days since LMP to current date) based on today's date and the LMP.

I found this code online at a website that calculates Gestational age in
online calculator. Can it be converted for use in access?
Thanks!


<script>

// current equation code
function PregDates() {

var lmpid = $("#lmp").val();
var ddid = $("#duedates").val();

if (ddid && lmpid) {alert('Please clear either the LMP or EDC box so we know
which one you\'re using. Thanks!'); return false;}

if (ddid) {
var today = Date.today();
var dd = Date.parse(ddid);

var lmp = Date.parse(ddid).add(-40).weeks();
lmps=lmp.toString("ddd M/d/yyyy");
var conception = Date.parse(ddid).add(-38).weeks();
conception = conception.toString("ddd M/d/yyyy");
var ega=today-lmp;
ega=ega/86400000;


var weeks = parseInt(ega/7);

if (weeks > 42) {alert("Either the pregnancy is past 42 weeks and that baby
needs to come out NOW, or the LMP is wrong or you've entered it wrong.
Double-check."); return false;}

var days = ega %7;
days = Math.round(days*1)/1;

if (days == 7) {
weeks=weeks+1;
var result = weeks + ' weeks exactly';

}

else {
var result = weeks + ' weeks and ' + days + '/7 days';

}

Done('#result',result);
Done('#result2',lmps);
Done('#result3',' ');
Done('#result4',conception);
}






if (lmpid) {
var today = Date.today();
var lmp = Date.parse(lmpid);

if (lmp > today) {alert("Hmm. The LMP shouldn't be after today. Check the
year.");return false;}

var edd = Date.parse(lmpid).add(40).weeks();
edd= edd.toString("ddd M/d/yyyy");
var conception = Date.parse(lmpid).add(14).days();
conception = conception.toString("ddd M/d/yyyy");
var ega=today-lmp;
ega=ega/86400000;


var weeks = parseInt(ega/7);

if (weeks > 42) {alert("Either the pregnancy is past 42 weeks and that baby
needs to come out NOW, or the LMP is wrong or you've entered it wrong.
Double-check."); return false;}

var days = ega %7;
days = Math.round(days*1)/1;

if (days == 7) {
weeks=weeks+1;
var result = weeks + ' weeks exactly';

}

else {
var result = weeks + ' weeks and ' + days + '/7 days';

}

Done('#result',result);
Done('#result2',' ');
Done('#result3',edd);
Done('#result4',conception);
}
}// end current equation code


</script>
 
D

Daryl S

Dr. Momo -

Here is a function you can use by passing in the LMP as a date. It will
return a string with the gestation weeks and days. This is based on the code
you had in your posting.

Public Function PregDates(lmpid As Date) As String

Dim EstDueDate As Date
Dim ConceptionDate As Date
Dim GestationWeeks As Integer
Dim GestationDays As Integer

If (lmpid > Date) Then
MsgBox ("lmp date cannot be after today")
PregDates = "Invalid lmp date entered"
Exit Function
End If

EstDueDate = DateAdd("ww", 40, lmpid)
ConceptionDate = DateAdd("d", 14, lmpid)
GestationWeeks = DateDiff("ww", lmpid, Date)
GestationDays = DateDiff("d", lmpid, Date)

If GestationWeeks > 42 Then
MsgBox ("Either the pregnancy is past 42 weeks and that baby needs to
come out NOW, or the LMP is wrong or you've entered it wrong. Double-check.")
PregDates = "Invalid Date or past term - " & GestationWeeks & " weeks."
Exit Function
End If

PregDates = "Gestation = " & GestationDays & " days or " & GestationWeeks &
" weeks and " & GestationDays - (GestationWeeks * 7) & " days."
End Function
 
D

Dr. Momo

You're awesome! Thanks for doing that!. I can't wait to try it!.
I'll let you know. Just to clarify: the only text box: lmpid and the
resultant form box where the function code goes is in Pregdates?
 
D

Daryl S

Dr. Momo -

This function should go into a module or at least behind the form you are
using. The lmpid should be a date field, so if it is text, convert it to
date when you pass it in to the function. You should have a text field on
your form to receive the string response, and the button you press to do the
calculation should have a statement like this:

Me.textboxonform = PregDates(Me.lmpid)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top