jQuery或javascript年龄计算器

时间:2012-09-03 07:50:33

标签: javascript jquery

我想要jQuery或javascript年龄计算器,在选择生日时设置用户的年龄,我希望从她的年龄开始显示最近几天,几个月和几年。

例如:

  

用户生日是: 29/04/2010
结果应该是这样的:    2年,4个月,5天。

通过jQuery或javascript执行此操作的最佳方法是什么?

2 个答案:

答案 0 :(得分:0)

我认为这可以通过使用Javascript来满足您的需求:

http://www.javascriptkit.com/script/script2/calculateage.shtml

答案 1 :(得分:0)

function calAge()
{



    var curday = $("#curDay").val();
    var curmon = $("#curMonth").val();
    var curyear = $("#curYear").val();


    var calday = $("#calDay").val();
    var calmon = $("#calMonth").val();
    var calyear = $("#calYear").val();



    if (curday == "" || curmon == "" || curyear == "" || calday == "" || calmon == "" || calyear == "")
    {
        alert("Please fill all the values and click go");
        return false;
    }


    if (parseFloat(calyear) > parseFloat(curyear))
    {
        alert("Enter your date of birth year less than current year");
        return false;
    }

    if (parseFloat(calyear) == parseFloat(curyear) && parseFloat(calmon) > parseFloat(curmon))
    {
        alert("Enter your date of birth month less than current month");
        return false;
    }


    if (parseFloat(calyear) == parseFloat(curyear) && parseFloat(calmon) == parseFloat(curmon) && parseFloat(calday) > parseFloat(curday))
    {
        alert("Enter your date of birth date less than current date");
        return false;
    }




    if (curday == calday && curmon == calmon && curyear == calyear)
    {
        alert("Happy Birthday...!");
        return false;
    }




    var curd = new Date(curyear, curmon - 1, curday);
    var cald = new Date(calyear, calmon - 1, calday);







    if (cald.getMonth() != (calmon - 1))
    {
        alert("Enter your valid date of birth");
        return false;
    }

    if (curd.getMonth() != (curmon - 1))
    {
        alert("Enter your valid date");
        return false;
    }

    if (curd.getFullYear() < cald.getFullYear())
    {
        alert("Enter your valid date");
        return false;
    }





    var diff = Date.UTC(curyear, curmon - 1, curday, 0, 0, 0) - Date.UTC(calyear, calmon - 1, calday, 0, 0, 0);




    var dife = datediff(cald, curd);


    console.log(dife);

    $(".your_age").text(dife.years + " years " + dife.months + " months " + dife.days + " days");

    var secleft = diff / 1000 / 60;
    $(".your_age_min").text(secleft + " minutes");

    var hrsleft = secleft / 60;
    $(".your_age_hour").text(hrsleft + " hours");

    var daysleft = hrsleft / 24;
    $(".your_age_days").text(daysleft + " days");

    // var secondsleft = secleft * 60;
    // $("#r8").val(secondsleft + " seconds");


    var as = parseInt(calyear) + dife.years + 1;

    var diff = Date.UTC(as, calmon - 1, calday, 0, 0, 0) - Date.UTC(curyear, curmon - 1, curday, 0, 0, 0);
    var datee = diff / 1000 / 60 / 60 / 24;


    if ((curday == calday) && (curmon == calmon))
    {
        $(".your_birthday span").html("Happy Birthday");
    } else
    {
        $(".your_birthday span").html(datee + " days left until your next birthday");
    }

    return true;



}

参考:https://www.quizexpo.com/how-old-am-i-age-calculator/