Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Compute Estimated Gestational Age from Due Date (Javascript)
// Usage: computeEGA(2014, 4, 14);
// Computes the Estimated Gestational Age of a patient based on today's date and her Expected Due Date (EDD)
// Sample data
var iDueDateYear = 2014;
var iDueDateMonth = 4;
var iDueDateDay = 14;
function computeEGA( iDueDateYear, iDueDateMonth, iDueDateDay ) {
var dToday = new Date();
var dDueDate = new Date( iDueDateYear, iDueDateMonth-1, iDueDateDay );
var iDaysUntilDueDate = (dDueDate - dToday) / (1000 * 60 * 60 * 24 );
var iTotalDaysInPregnancy = 40*7;
var iGestationalAgeInDays = iTotalDaysInPregnancy - iDaysUntilDueDate;
var fGestationalAgeInWeeks = iGestationalAgeInDays / 7;
var iEGAWeeks = Math.floor( fGestationalAgeInWeeks );
var iEGADays = ((fGestationalAgeInWeeks % 1)*7).toFixed(0)
console.log( iEGAWeeks + " weeks" );
console.log( iEGADays + " days" );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.