如何从jQuery datepicker中计算年龄? [英] How to calculate age from jquery datepicker?

查看:63
本文介绍了如何从jQuery datepicker中计算年龄?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想计算使用jQuery日期选择器选择日期时的年龄。我在下面添加了代码,但是如果我选择 19/03/2015, 15/01/2015或 19/03/2014, 31/12/2014之类的日期,它将显示负值

I want to calculate age when date is selected by using jquery date picker. I added code below but it showing minus value if i select date like '19/03/2015','15/01/2015' or '19/03/2014' ,'31/12/2014'

  $(document).ready(function () 
{
 console.log($(document).width());           
     $('#patientDob').datepicker
    ({
        dateFormat: 'dd/mm/yy',
        changeMonth: true,
        changeYear: true,
        yearRange: '1900:2150',
        maxDate: new Date(),
        inline: true,

             onSelect: function() {
               var birthDay = document.getElementById("patientDob").value;
                var DOB = new Date(birthDay);
                var today = new Date();
                var age = today.getTime() - DOB.getTime();
                age = Math.floor(age / (1000 * 60 * 60 * 24 * 365.25));

                document.getElementById('patientAge').value = age;
            }
     });  

});


推荐答案

AFAIK JavaScript日期需要yyyy / mm / dd,并且您正在发送:

AFAIK The javascript Date requires yyyy/mm/dd, and you are sending:

 var DOB = new Date(birthDay); //  dateFormat: 'dd/mm/yy'

将格式更改为 yyyy / mm / dd并且可以正常工作。

Change format to "yyyy/mm/dd" and will work ok.

这篇关于如何从jQuery datepicker中计算年龄?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆