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.
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 {}
});
});
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
Benutze [momentjs] (http://momentjs.com/docs/#/manipulating/add/), um die Daten zu berechnen –
@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. –