Last active
December 14, 2015 13:58
-
-
Save lanqy/5097056 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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