Javascript 以月和日为单位计算年龄 函数getAge(日期字符串){ var today=新日期(); var birthDate=新日期(日期字符串); var age=today.getFullYear()-birthDate.getFullYear(); var m=today.getMonth()-birthDate.getMonth(); 如果(m0)age.push(ddiff+'day'+(ddiff>1's':''); 如果(年龄长度>1)年龄拼接(年龄长度-1,0,'和'); 返回年龄。加入(“”); } getAge(“1974年1月25日”)>>37岁8个月零26天 getAge(“1984年9月15日”)>>27岁1个月零5天 getAge(“1984年12月20日”、“2011年10月20日”)>>26岁零9个月 getAge(新日期(),“2011年12月25日”)+“直到圣诞节”>> 离圣诞节还有2个月零5天 getAge(“2011年6月25日”)>>3个月零25天 你到底想要什么。离下一个生日还有几个月?或者人的年龄?请澄清。谢谢。我认为应该有(如果解决方案中有错误。假设当前日期是2016年2月16日,你将出生日期传递为2015年2月17日。然后上面的代码返回27天的年龄。这个答案对我帮助很大兄弟:) function getAge(fromdate, todate){ if(todate) todate= new Date(todate); else todate= new Date(); var age= [], fromdate= new Date(fromdate), y= [todate.getFullYear(), fromdate.getFullYear()], ydiff= y[0]-y[1], m= [todate.getMonth(), fromdate.getMonth()], mdiff= m[0]-m[1], d= [todate.getDate(), fromdate.getDate()], ddiff= d[0]-d[1]; if(mdiff < 0 || (mdiff=== 0 && ddiff<0))--ydiff; if(mdiff<0) mdiff+= 12; if(ddiff<0){ fromdate.setMonth(m[1]+1, 0); ddiff= fromdate.getDate()-d[1]+d[0]; --mdiff; } if(ydiff> 0) age.push(ydiff+ ' year'+(ydiff> 1? 's ':' ')); if(mdiff> 0) age.push(mdiff+ ' month'+(mdiff> 1? 's':'')); if(ddiff> 0) age.push(ddiff+ ' day'+(ddiff> 1? 's':'')); if(age.length>1) age.splice(age.length-1,0,' and '); return age.join(''); } getAge("1/25/1974")>> 37 years 8 months and 26 days getAge("9/15/1984")>> 27 years 1 month and 5 days getAge("12/20/1984","10,20,2011")>>26 years and 9 months getAge(new Date(),"12/25/2011")+' till Christmas'>> 2 months and 5 days till Christmas getAge("6/25/2011")>> 3 months and 25 days

Javascript 以月和日为单位计算年龄 函数getAge(日期字符串){ var today=新日期(); var birthDate=新日期(日期字符串); var age=today.getFullYear()-birthDate.getFullYear(); var m=today.getMonth()-birthDate.getMonth(); 如果(m0)age.push(ddiff+'day'+(ddiff>1's':''); 如果(年龄长度>1)年龄拼接(年龄长度-1,0,'和'); 返回年龄。加入(“”); } getAge(“1974年1月25日”)>>37岁8个月零26天 getAge(“1984年9月15日”)>>27岁1个月零5天 getAge(“1984年12月20日”、“2011年10月20日”)>>26岁零9个月 getAge(新日期(),“2011年12月25日”)+“直到圣诞节”>> 离圣诞节还有2个月零5天 getAge(“2011年6月25日”)>>3个月零25天 你到底想要什么。离下一个生日还有几个月?或者人的年龄?请澄清。谢谢。我认为应该有(如果解决方案中有错误。假设当前日期是2016年2月16日,你将出生日期传递为2015年2月17日。然后上面的代码返回27天的年龄。这个答案对我帮助很大兄弟:) function getAge(fromdate, todate){ if(todate) todate= new Date(todate); else todate= new Date(); var age= [], fromdate= new Date(fromdate), y= [todate.getFullYear(), fromdate.getFullYear()], ydiff= y[0]-y[1], m= [todate.getMonth(), fromdate.getMonth()], mdiff= m[0]-m[1], d= [todate.getDate(), fromdate.getDate()], ddiff= d[0]-d[1]; if(mdiff < 0 || (mdiff=== 0 && ddiff<0))--ydiff; if(mdiff<0) mdiff+= 12; if(ddiff<0){ fromdate.setMonth(m[1]+1, 0); ddiff= fromdate.getDate()-d[1]+d[0]; --mdiff; } if(ydiff> 0) age.push(ydiff+ ' year'+(ydiff> 1? 's ':' ')); if(mdiff> 0) age.push(mdiff+ ' month'+(mdiff> 1? 's':'')); if(ddiff> 0) age.push(ddiff+ ' day'+(ddiff> 1? 's':'')); if(age.length>1) age.splice(age.length-1,0,' and '); return age.join(''); } getAge("1/25/1974")>> 37 years 8 months and 26 days getAge("9/15/1984")>> 27 years 1 month and 5 days getAge("12/20/1984","10,20,2011")>>26 years and 9 months getAge(new Date(),"12/25/2011")+' till Christmas'>> 2 months and 5 days till Christmas getAge("6/25/2011")>> 3 months and 25 days,javascript,Javascript,这显示了我有0年了,但我想显示10个月和9个月,直到他的生日10/20/2011 有了它,您可以有一个描述性的年龄文本:天、月和年: function getAge(dateString) { var today = new Date(); var birthDate = new Date(dateString); var age = today.getFullYear() - birthDate.getFullYear(); var m = today.getM

这显示了我有0年了,但我想显示
10个月
和9个月,直到他的生日
10/20/2011

有了它,您可以有一个描述性的年龄文本:天、月和年:

function getAge(dateString) {
    var today = new Date();
    var birthDate = new Date(dateString);
    var age = today.getFullYear() - birthDate.getFullYear();
    var m = today.getMonth() - birthDate.getMonth();
    if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
        age--;
    }
    document.write(age);

}

getAge("01/20/2011")
函数getAge(日期字符串){
var birthdate=新日期(dateString).getTime();
var now=new Date().getTime();
//现在找出现在和生日之间的区别
var n=(现在-出生日期)/1000;
如果(n<604800){//少于一周
var day_n=数学楼层(n/86400);
返回日+“日”+(日>1“s”:“;
}如果(n<2629743){//少于一个月
var week_n=数学下限(n/604800);
返回周+“周”+(周>1“s”:“”;
}如果(n<63113852){//少于24个月
变量月=数学下限(n/2629743);
返回月份+'月份'+(月份>1's':'';
}否则{
变量年份=数学下限(n/31556926);
返回年份+年份+(年份>1?'s':“”);
}
}
var年龄=getAge(“2011年1月20日”);
警觉(年龄);

你经常想知道一个人在特定日期的年龄, 或者两个日期之间的年、月和日

如果您确实想要截止到今天的年龄,请传递一个日期或日期字符串参数

function getAge(dateString) {

  var birthdate = new Date(dateString).getTime();
  var now = new Date().getTime();
  // now find the difference between now and the birthdate
  var n = (now - birthdate)/1000;

  if (n < 604800) { // less than a week
    var day_n = Math.floor(n/86400);
    return day_n + ' day' + (day_n > 1 ? 's' : '');
  } else if (n < 2629743) {  // less than a month
    var week_n = Math.floor(n/604800);
    return week_n + ' week' + (week_n > 1 ? 's' : '');
  } else if (n < 63113852) { // less than 24 months
    var month_n = Math.floor(n/2629743);
    return month_n + ' month' + (month_n > 1 ? 's' : '');
  } else { 
    var year_n = Math.floor(n/31556926);
    return year_n + ' year' + (year_n > 1 ? 's' : '');
  }
}


var age = getAge("01/20/2011");
alert(age);
函数getAge(fromdate,todate){
如果(todate)todate=新日期(todate);
else todate=新日期();
var age=[],fromdate=新日期(fromdate),
y=[todate.getFullYear(),fromdate.getFullYear()],
ydiff=y[0]-y[1],
m=[todate.getMonth(),fromdate.getMonth()],
mdiff=m[0]-m[1],
d=[todate.getDate(),fromdate.getDate()],
ddiff=d[0]-d[1];
如果(mdiff<0 | |(mdiff==0&&ddiff 0)age.push(mdiff+'month'+(mdiff>1's':'');
如果(ddiff>0)age.push(ddiff+'day'+(ddiff>1's':'');
如果(年龄长度>1)年龄拼接(年龄长度-1,0,'和');
返回年龄。加入(“”);
}
getAge(“1974年1月25日”)>>37岁8个月零26天
getAge(“1984年9月15日”)>>27岁1个月零5天
getAge(“1984年12月20日”、“2011年10月20日”)>>26岁零9个月
getAge(新日期(),“2011年12月25日”)+“直到圣诞节”>>
离圣诞节还有2个月零5天
getAge(“2011年6月25日”)>>3个月零25天

你到底想要什么。离下一个生日还有几个月?或者人的年龄?请澄清。谢谢。我认为应该有(如果解决方案中有错误。假设当前日期是2016年2月16日,你将出生日期传递为2015年2月17日。然后上面的代码返回27天的年龄。这个答案对我帮助很大兄弟:)
function getAge(fromdate, todate){
    if(todate) todate= new Date(todate);
    else todate= new Date();

    var age= [], fromdate= new Date(fromdate),
    y= [todate.getFullYear(), fromdate.getFullYear()],
    ydiff= y[0]-y[1],
    m= [todate.getMonth(), fromdate.getMonth()],
    mdiff= m[0]-m[1],
    d= [todate.getDate(), fromdate.getDate()],
    ddiff= d[0]-d[1];

    if(mdiff < 0 || (mdiff=== 0 && ddiff<0))--ydiff;
    if(mdiff<0) mdiff+= 12;
    if(ddiff<0){
        fromdate.setMonth(m[1]+1, 0);
        ddiff= fromdate.getDate()-d[1]+d[0];
        --mdiff;
    }
    if(ydiff> 0) age.push(ydiff+ ' year'+(ydiff> 1? 's ':' '));
    if(mdiff> 0) age.push(mdiff+ ' month'+(mdiff> 1? 's':''));
    if(ddiff> 0) age.push(ddiff+ ' day'+(ddiff> 1? 's':''));
    if(age.length>1) age.splice(age.length-1,0,' and ');    
    return age.join('');
}


getAge("1/25/1974")>> 37 years 8 months and 26 days

getAge("9/15/1984")>> 27 years 1 month and 5 days

getAge("12/20/1984","10,20,2011")>>26 years  and 9 months

getAge(new Date(),"12/25/2011")+' till Christmas'>>
2 months and 5 days till Christmas

getAge("6/25/2011")>> 3 months and 25 days