Skip to content

Instantly share code, notes, and snippets.

@lanqy
Last active December 14, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lanqy/5097056 to your computer and use it in GitHub Desktop.
Save lanqy/5097056 to your computer and use it in GitHub Desktop.
// from http://stackoverflow.com/questions/5786186/javascript-age-count-from-date-of-birth
function date2Age(str){ // 根据年月日计算年龄str 格式为yyyy-mm-dd
var diff = new Date - new Date(parseISO8601(str));
var diffdays = diff / 1000 / (60 * 60 * 24);
return Math.floor(diffdays / 365.25)
}
//form http://stackoverflow.com/questions/2182246/javascript-dates-in-ie-nan-firefox-chrome-ok
function parseISO8601(dateStringInRange) { // fixed IE8 NaN
var isoExp = /^\s*(\d{4})-(\d\d)-(\d\d)\s*$/,
date = new Date(NaN),
month,
parts = isoExp.exec(dateStringInRange);
if (parts) {
month = +parts[2];
date.setFullYear(parts[1], month - 1, parts[3]);
if (month != date.getMonth() + 1) {
date.setTime(NaN);
}
}
return date;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment