这个不知道有没有项目会用。。反正我遇到了。。。写了一份留着吧。。

根据日期获取年龄(传入yyyy-mm-dd):

//根据日期获取年龄(传入yyyy-mm-dd)
function getAgeByBornDate(bornDate) {
    var myDate = new Date();
    var month = myDate.getMonth() + 1;
    var day = myDate.getDate();
    var age = myDate.getFullYear() - parseInt(bornDate.substring(0, 4)) - 1;
    if (parseInt(bornDate.substring(5, 7)) < month ||
        parseInt(bornDate.substring(5, 7)) == month && parseInt(bornDate.substring(8, 10)) <= day) {
        age++;
    }
    return age;
}

根据日期获取星座(传入yyyy-mm-dd):

//根据日期获取星座(传入yyyy-mm-dd)
function getConstellationByBornDate(bornDate) {
    var month = parseInt(bornDate.substring(5, 7));
    var day = parseInt(bornDate.substring(8, 10));

    if ((month == 1 && day >= 20) || (month == 2 && day <= 18)) {
        return "水瓶座";
    } else if ((month == 2 && day >= 19) || (month == 3 && day <= 20)) {
        return "双鱼座";
    } else if ((month == 3 && day >= 21) || (month == 4 && day <= 19)) {
        return "白羊座";
    } else if ((month == 4 && day >= 20) || (month == 5 && day <= 20)) {
        return "金牛座";
    } else if ((month == 5 && day >= 21) || (month == 6 && day <= 21)) {
        return "双子座";
    } else if ((month == 6 && day >= 22) || (month == 7 && day <= 22)) {
        return "巨蟹座";
    } else if ((month == 7 && day >= 23) || (month == 8 && day <= 22)) {
        return "狮子座";
    } else if ((month == 8 && day >= 23) || (month == 9 && day <= 22)) {
        return "处女座";
    } else if ((month == 9 && day >= 23) || (month == 10 && day <= 23)) {
        return "天秤座";
    } else if ((month == 10 && day >= 24) || (month == 11 && day <= 22)) {
        return "天蝎座";
    } else if ((month == 11 && day >= 23) || (month == 12 && day <= 21)) {
        return "射手座";
    } else if ((month == 12 && day >= 22) || (month == 1 && day <= 19)) {
        return "摩羯座";
    }
    return;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注