Editing age calc This edit will create a new revision. Your details (optional) Name Email (won’t be displayed; might be used for Gravatar) URL Test case details Title * Published (uncheck if you want to fiddle around before making the page public) Description (in case you feel further explanation is needed)(Markdown syntax is allowed) Are you a spammer? (just answer the question) Preparation code Preparation code HTML (this will be inserted in the <body> of a valid HTML5 document in standards mode) (useful when testing DOM operations or including libraries) Include JavaScript libraries as follows: <script src="//cdn.ext/library.js"></script> Define setup for all tests (variables, functions, arrays or other objects that will be used in the tests) (runs before each clocked test loop, outside of the timed code region) (e.g. define local test variables, reset global variables, clear canvas, etc.) (see FAQ) function ngetage(a) { a = new Date(Date.parse(a.replace(/-/g, "/"))); var b = new Date, years = b.getYear() - a.getYear(), months = b.getMonth() - a.getMonth(), days = b.getDate() - a.getDate(); if (b <= a) return "Invalid DOB." months < 0 && (years--, months + 12); days < 0 && (months--, days += 31); months < 0 && (years--, months = 11); a = [], b = function(b, c) { b > 0 && a.push(b + c + (b > 1 ? "s" : "")) } b(years, " year"); b(months, " month"); b(days, " day"); a.length > 1 && (a[a.length - 1] = "and " + a[a.length - 1]); return (!a.length ? "0 days" : "") + a.join(a.length > 2 ? ", " : " ") + " old." } function ogetage(dateString) { var now = new Date(); var today = new Date(now.getYear(), now.getMonth(), now.getDate()); var yearNow = now.getYear(); var monthNow = now.getMonth(); var dateNow = now.getDate(); var dob = new Date(dateString.substring(0, 4), dateString.substring(5, 7) - 1, dateString.substring(8, 11)); var yearDob = dob.getYear(); var monthDob = dob.getMonth(); var dateDob = dob.getDate(); var age = {}; var ageString = ""; var yearString = ""; var monthString = ""; var dayString = ""; yearAge = yearNow - yearDob; if (monthNow >= monthDob) var monthAge = monthNow - monthDob; else { yearAge--; var monthAge = 12 + monthNow - monthDob; } if (dateNow >= dateDob) var dateAge = dateNow - dateDob; else { monthAge--; var dateAge = 31 + dateNow - dateDob; if (monthAge < 0) { monthAge = 11; yearAge--; } } age = { years: yearAge, months: monthAge, days: dateAge }; if (age.years > 1) yearString = " years"; else yearString = " year"; if (age.months > 1) monthString = " months"; else monthString = " month"; if (age.days > 1) dayString = " days"; else dayString = " day"; if ((age.years > 0) && (age.months > 0) && (age.days > 0)) ageString = age.years + yearString + ", " + age.months + monthString + ", and " + age.days + dayString + " old."; else if ((age.years == 0) && (age.months == 0) && (age.days > 0)) ageString = "Only " + age.days + dayString + " old!"; else if ((age.years > 0) && (age.months == 0) && (age.days == 0)) ageString = age.years + yearString + " old. Happy Birthday!!"; else if ((age.years > 0) && (age.months > 0) && (age.days == 0)) ageString = age.years + yearString + " and " + age.months + monthString + " old."; else if ((age.years == 0) && (age.months > 0) && (age.days > 0)) ageString = age.months + monthString + " and " + age.days + dayString + " old."; else if ((age.years > 0) && (age.months == 0) && (age.days > 0)) ageString = age.years + yearString + " and " + age.days + dayString + " old."; else if ((age.years == 0) && (age.months > 0) && (age.days == 0)) ageString = age.months + monthString + " old."; else ageString = "Oops! Could not calculate age!"; return ageString; } Define teardown for all tests (runs after each clocked test loop, outside of the timed code region) (see FAQ) Code snippets to compare Test 1 Title Async (check if this is an asynchronous test) Code ogetage("1994-10-25"); ogetage("2012-12-14"); ogetage("2001-12-28"); ogetage("1995-02-28"); ogetage("2012-02-26"); ogetage("2008-12-23"); ogetage("2012-07-28"); ogetage("2012-12-28"); Test 2 Title Async (check if this is an asynchronous test) Code ngetage("1994-10-25"); // 18 years, 2 months, and 3 days old. ngetage("2012-12-14"); // 14 days old. ngetage("2001-12-28"); // 11 years old. ngetage("1995-02-28"); // 17 years and 10 months old. ngetage("2012-02-26"); // 10 months and 2 days old. ngetage("2008-12-23"); // 4 years and 5 days old. ngetage("2012-07-28"); // 5 months old. ngetage("2012-12-28"); // 0 days old.