Javascript jQuery年龄计算未计算闰年
Iam通过保留今天日期和所选日期来计算年龄 如果我今天(2016年4月28日)说,-准确显示了3年 如果我说昨天(2016年4月27日)-仍然显示了3年的时间,这不应该是因为3年中1天的时间更短。我不知道我的代码哪里做错了 谢谢你的帮助 示例代码:Javascript jQuery年龄计算未计算闰年,javascript,jquery,calculator,Javascript,Jquery,Calculator,Iam通过保留今天日期和所选日期来计算年龄 如果我今天(2016年4月28日)说,-准确显示了3年 如果我说昨天(2016年4月27日)-仍然显示了3年的时间,这不应该是因为3年中1天的时间更短。我不知道我的代码哪里做错了 谢谢你的帮助 示例代码: var getMonth, getDay, getYear, getDate, dob, today, age; $(document).on('change', '#node_enfamama_registration_form_form_gro
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 {}
});
});
var getMonth、getDay、getYear、getDate、dob、today、age;
$(文档).on('change','#节点(enfamama)注册(form)表格(group)表格(enr)隐藏(child)信息(form select),函数({
$(this).each(function(){
if($(this).parents().hasClass('date-month')){
getMonth=$(this.val();
警报(getMonth)
}
else if($(this.parents().hasClass('date-day')){
getDay=$(this.val();
警报(getDay)
}
else if($(this.parents().hasClass('date-year')){
getYear=$(this.val();
警报(getYear)
getDate=getYear+“-”+getMonth+“-”+getDay;
警报(“月、日和年”+getDate)
$('.morerar msg').remove();
$('.less than msg').remove();
dob=新日期(getDate);
今天=新日期();
年龄=数学楼层((今日-dob)/(365.25*24*60*60*1000));
警报(“儿童年龄为”+年龄)
//调试器;
/*
var生日=新日期(getDate);
var age=today.getFullYear()-birthDate.getFullYear();
var m=today.getMonth()-birthDate.getMonth();
如果(m<0 | |(m==0&&today.getDate()3岁){
//调试器;
$(this).parents('.fieldset wrapper')。after('您也可以访问以了解如何继续为您的孩子提供360度优势')
}else if(age=0&&ageMath.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))
我已经解决了这个问题
(函数($){
Drupal.behaviors.addChild={
附加:功能(上下文、设置){
变量getMonth、getDay、getYear、getDate、dob、today、age、contentParent;
$(“#节点(enfamama)注册(form)表单(form)组(enr)隐藏(child)信息(form select)。)更改(函数({
if($(this).parents().hasClass('date-month')){
getMonth=$(this.val();
contentParent=$(this.parents('.draggable');
如果(getMonth==undefined | | | getMonth==null | | | getDay==undefined | | getDay==null | | getYear==undefined | | getYear==null |其他{
年龄计算();
}
}else if($(this.parents().hasClass('date-day')){
getDay=$(this.val();
contentParent=$(this.parents('.draggable');
如果(getMonth==undefined | | | getMonth==null | | | getDay==undefined | | getDay==null | | getYear==undefined | | getYear==null |其他{
年龄计算()
}
}else if($(this.parents().hasClass('date-year')){
getYear=$(this.val();
contentParent=$(this.parents('.draggable');
如果(getMonth==undefined | | | getMonth==null | | | getDay==undefined | | getDay==null | | getYear==undefined | | getYear==null |其他{
年龄计算()
}
}else{}
});
函数计算(){
getDate=getYear+“-”+getMonth+“-”+getDay;
dob=新日期(getDate);
今天=新日期();
年龄=(今日-dob)/(365.25*24*60*60*1000);
如果(年龄<3 | |年龄==3 | |年龄>3和年龄<3.0045242229471007){
$('.greater msg,.less than msg').remove();
$(contentParent).find(“.fieldset wrapper”).after('免责声明:根据EO51,不能直接与孩子年龄在0到3岁之间的母亲联系。您将通过电子邮件收到的所有内容将仅与您的怀孕有关')
}否则,如果(年龄>3岁){
$('.greater msg,.less than msg').remove();
$(contentParent).find(“.fieldset wrapper”).after('您也可以访问以了解如何继续为您的孩子提供360度优势')
}
如果(年龄)你能更具体地说明你是如何测试的吗?我已经做了以下工作:选择2013年4月28日=>我的孩子是3。正确。选择2013年4月27日=>3正确(因为他是3岁1天)。选择2013年4月29日=>2正确(再过一天是3)。然后我在javascript中将今天设置为昨天(今天=新日期('2016-04-27'))并选择2013年4月28日=>2。用于计算dates@Laura:谢谢,正确,选择2013年4月27日=>3正确(因为他已经3岁1天了),然后应该显示提醒(“你也可以访问”)消息。我现在明白你的意思了。这是因为你使用了age=Math.floor,它将是3。而不是3,什么的。
(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);