原生js利用时间戳计算出具体的年龄

代码

function getAge(birthday)
{
    //出生时间 毫秒
    var birthDayTime = new Date(birthday).getTime(); 
    //当前时间 毫秒
    var nowTime = new Date().getTime(); 
    //一年毫秒数(365 * 86400000 = 31536000000)
    return Math.ceil((nowTime-birthDayTime)/31536000000);
}

注:Math.ceil为向上取整

Last modification:January 31st, 2020 at 11:06 am
If you think my article is useful to you, please feel free to appreciate

Leave a Comment