2016-04-28 8 views
2

Ich berechne Alter, indem Sie das heutige Datum und das ausgewählte Datum beibehalten.jQuery Altersberechnung berechnet nicht Leap Years

wenn ich sagen heute (28. April 2016) - Exact 3 Jahre ist die Anzeige korrekt

wenn ich sagen gestern (27. April 2016) - Noch 3 Jahre ist anzeigt, was nicht sein sollte als 1 Tag ist weniger als 3 Jahre. Ich weiß nicht, wo ich in meinem Code falsch mache.

Age Calculator DEMO

Schätzen Sie Ihre Hilfe!

Beispielcode:

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

Können Sie das konkretisieren sein, wie Sie testen? Ich habe folgendes getan: Auswahl 28-April-2013 => mein Kind ist 3. richtig. Auswahl 27-April-2013 => 3 richtig (da er 3 und 1 Tag ist). Auswahl 29-April-2013 => 2 richtig (ein weiterer Tag bis 3). ich habe dann heute im javascript wie gestern eingestellt (heute = neues Datum ('2016-04-27')) und ausgewählt 28.4.2013 => 2 richtig. – Laura

+2

Benutze [momentjs] (http://momentjs.com/docs/#/manipulating/add/), um die Daten zu berechnen –

+0

@Laura: Danke, richtig, Auswahl 27-April-2013 => 3 richtig (da er 3 ist und 1 Tag), dann Alarm ("Sie können auch besuchen") Nachricht sollte angezeigt werden. –

Antwort

1

Math.floor() wird immer eine ganze Zahl zurück. Wenn du machst = Math.floor (...), wird das 3 sein, nicht 3.005. Verwenden Sie Math.floor() nur, wenn Sie den Wert anzeigen, aber vergleichen Sie das tatsächliche Ergebnis in Ihrem if. es so versuchen:

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

Danke, Das funktioniert gut, aber wenn ich das heutige Datum auswähle, das gleich 3 ist, dann (Disclaimer) Nachricht muss kommen, Update fiddle- https://jsfiddle.net/stanze/nyp5waya/1/ –

+0

Das stimmt, Sie habe ein Problem für den genauen Tag. Sie müssen irgendwie wissen, ob das Kind an diesem Tag 3 wird. Ein Weg wäre, nicht mit 3 zu vergleichen, sondern einen genaueren Wert zu verwenden. Zum Beispiel, ein kleiner Test, den ich gemacht habe: wenn heute, Alter = 3.002 ..., wenn gestern Alter = 3.005 ... Also könnte man es irgendwie auf 3 Dezimalstellen runden und das benutzen. Aber dann wieder, ich bin mir sicher, dass es einen schlaueren Weg gibt, aber ich kann es jetzt nicht herausfinden: D – Laura

+0

Danke nochmal, werde einen Weg finden :-) –

0

ich das Problem behoben haben,

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