2016-04-28 185 views
2

Iam通過保留今天日期和選定日期來計算年齡。jQuery年齡計算不計算閏年

如果我說今天(28日2016年4月) - 精確3年正確顯示

如果我說昨日(27日2016年4月) - 仍然3年正在顯示,這不應該是因爲1天較少爲3年。我不知道iam在我的代碼中做錯了什麼。

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 {} 


      }); 
     }); 
+0

你能具體談談你如何測試?我做了以下事情:選擇28-april-2013 =>我的孩子是3.正確的。選擇27-april-2013 => 3正確(因爲他是3天和1天)。選擇29-april-2013 => 2正確(多一天到三天)。然後我今天在JavaScript中設置爲昨天(今天=新日期('2016-04-27')),並選擇2013年4月28日=> 2正確。 – Laura

+2

使用[momentjs](http://momentjs.com/docs/#/manipulating/add/)來計算日期 –

+0

@Laura:謝謝,正確,選擇27-april-2013 => 3正確(因爲他是3和1天),然後警報(「你也可以訪問」)消息應該顯示。 –

回答

1

Math.floor()總是會返回一個整數。 當你做age = 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)) 
+0

謝謝,這工作正常,但如果我選擇今天的日期等於3那麼(免責聲明)消息必須來,更新小提琴 - https://jsfiddle.net/stanze/nyp5waya/1/ –

+0

這是真的,你確切的一天有問題。你需要知道當天孩子是否成爲3歲。一種方法不是與3進行比較,而是使用更精確的值。例如,我做了一個小測試:如果今天,年齡= 3.002 ...,如果昨天年齡= 3.005 ...所以你可以將它舍入到小數點後三位並使用它。但是,我確信有一個更聰明的方法,但現在無法弄清楚:D – Laura

+0

再次感謝,我們會找出一個辦法:-) –

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);