jQuery年龄计算不计算Leap Years

时间:2016-04-28 11:54:20

标签: javascript jquery calculator

Iam通过保持今天的日期和所选日期来计算年龄。

如果我说今天(2016年4月28日) - 确切的3年正确显示

如果我说昨天(2016年4月27日) - 仍然显示3年,这不应该是3天的1天。我不知道我的代码在哪里做错了。

Age Calculator DEMO

感谢您的帮助!

示例代码:

var getMonth, getDay, getYear, getDate, dob, today, age;
$(document).on('change', '#node_enfamama_registration_form_form_group_enr_hide_child_info .form-select', function() {
            $(this).each(function() {

               if ($(this).parents().hasClass('date-month')) {
                    getMonth = $(this).val();
                                        alert(getMonth)
                } 

                else if ($(this).parents().hasClass('date-day')) {
                    getDay = $(this).val();
                                        alert(getDay)
                }

                else if ($(this).parents().hasClass('date-year')) {
                    getYear = $(this).val();
                    alert(getYear)

                    getDate = getYear + "-" + getMonth + "-" + getDay;

                    alert("Month, day & year" + getDate)

                    $('.greater-msg').remove();
                    $('.less-then-msg').remove();               
                                        dob = new Date(getDate);
                    today = new Date();
                                        age = Math.floor((today - dob) / (365.25 * 24 * 60 * 60 * 1000));
                        alert("Child Age is " + age)
                    //debugger;

                    /*
                    var birthDate = new Date(getDate);
                    var age = today.getFullYear() - birthDate.getFullYear();
                    var m = today.getMonth() - birthDate.getMonth();
                    if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
                        alert("aaa" + age)

                    age--;
                    }*/


                    if (age > 3) {
                        //debugger;
                        $(this).parents('.fieldset-wrapper').after('<div class="greater-msg">You can also visit <a href="http://www.enfagrow4.com" target="_blank">www.enfagrow4.com</a> to know how you can keep giving your child the 360 advantage.</div>')
                    } else if (age <= -1) {
                        //$(this).parents('.fieldset-wrapper').after('<div class="less-then-msg">Less Disclaimer: In compliance with EO51, Mead Johnson Nutrition cannot directly engage with mothers with children aged 0 to 3 years old. All content that you will receive via email will only be regarding your pregnancy. </div>')
                    } else if (age >= 0 && age <= 3) {
                        $(this).parents('.fieldset-wrapper').after('<div class="less-then-msg">Less Disclaimer: In compliance with EO51, Mead Johnson Nutrition cannot directly engage with mothers with children aged 0 to 3 years old. All content that you will receive via email will only be regarding your pregnancy. </div>')
                    } else {
                    } 

                    function checkDate(){

                    }

                } else {}


            });
        });

2 个答案:

答案 0 :(得分:1)

Math.floor()将始终返回一个整数。 当你年龄= Math.floor(...)时,它将是3,而不是3.005。 在显示值时使用Math.floor(),但比较if中的实际结果。 试试这样:

    age = (today - dob) / (365.25 * 24 * 60 * 60 * 1000);
    alert(age);
    alert("Child Age is " + Math.floor(age))

答案 1 :(得分:0)

我已经解决了问题,

DEMO

(function($) {
    Drupal.behaviors.addChild = {
        attach: function(context, settings) {
            var getMonth, getDay, getYear, getDate, dob, today, age, contentParent;
            $('#node_enfamama_registration_form_form_group_enr_hide_child_info .form-select').change(function() {
                if ($(this).parents().hasClass('date-month')) {
                    getMonth = $(this).val();
                    contentParent = $(this).parents('.draggable');

                    if (getMonth == undefined || getMonth == "" || getMonth == null || getDay == undefined || getDay == "" || getDay == null || getYear == undefined || getYear == "" || getYear == null) {} else {
                        ageCalculation();
                    }

                } else if ($(this).parents().hasClass('date-day')) {
                    getDay = $(this).val();
                    contentParent = $(this).parents('.draggable');
                    if (getMonth == undefined || getMonth == "" || getMonth == null || getDay == undefined || getDay == "" || getDay == null || getYear == undefined || getYear == "" || getYear == null) {} else {
                        ageCalculation()
                    }

                } else if ($(this).parents().hasClass('date-year')) {
                    getYear = $(this).val();
                    contentParent = $(this).parents('.draggable');
                    if (getMonth == undefined || getMonth == "" || getMonth == null || getDay == undefined || getDay == "" || getDay == null || getYear == undefined || getYear == "" || getYear == null) {} else {
                        ageCalculation()
                    }

                } else {}
            });

            function ageCalculation() {
                getDate = getYear + "-" + getMonth + "-" + getDay;

                dob = new Date(getDate);
                today = new Date();
                age = (today - dob) / (365.25 * 24 * 60 * 60 * 1000);

                if (age < 3 || age == 3 || age > 3 && age < 3.00452422294471007) {
                    $('.greater-msg, .less-then-msg').remove();
                    $(contentParent).find('.fieldset-wrapper').after('<div class="less-then-msg">Disclaimer: In compliance with EO51,  cannot directly engage with mothers with children aged 0 to 3 years old. All content that you will receive via email will only be regarding your pregnancy. </div>')
                } else if (age > 3) {
                    $('.greater-msg, .less-then-msg').remove();
                    $(contentParent).find('.fieldset-wrapper').after('<div class="greater-msg">You can also visit <a href="http://www.enfagrow4.com">www.enfagrow4.com</a> to know how you can keep giving your child the 360 advantage.</div>')
                }
                if (age <= -1 || age <= -0 || age == 0 || age == -0) {
                    $('.greater-msg, .less-then-msg').remove();
                }
            }
        }
    };
})(jQuery);