最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to calculate age from jquery datepicker? - Stack Overflow

programmeradmin0浏览0评论

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

});

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

});
Share Improve this question edited Mar 19, 2015 at 5:27 zozo 8,61219 gold badges83 silver badges141 bronze badges asked Mar 19, 2015 at 5:26 Vg.Vg. 1332 gold badges3 silver badges17 bronze badges 2
  • What do you mean "it showing minus value"? – zozo Commented Mar 19, 2015 at 5:29
  • I seriously doubt that the only age problem is the minus value. For example new Date("31/12/2014") = 12 Jul 2016 ... read the answer (edited, had a problem last time) :). – zozo Commented Mar 19, 2015 at 5:52
Add a ment  | 

2 Answers 2

Reset to default 5

I have created this age calculator for my project using jQuery UI. and JavaScript function. you will get the exact result.

It it will calculate age and display as human readable. create a date field with ID 'datepicker'and import jquery and jquery ui . After that

Then just copy and paste the code to get the exact result.

output // 28 years 7 months 7 days

        $(function () {
        $("#datepicker").datepicker({
            changeMonth: true,
            changeYear: true,
            showAnim: 'slideDown',
            dateFormat: 'yy-mm-dd'
        }).on('change', function () {
            var age = getAge(this);
           /* $('#age').val(age);*/
            console.log(age);
            alert(age);

        });

        function getAge(dateVal) {
            var
                birthday = new Date(dateVal.value),
                today = new Date(),
                ageInMilliseconds = new Date(today - birthday),
                years = ageInMilliseconds / (24 * 60 * 60 * 1000 * 365.25 ),
                months = 12 * (years % 1),
                days = Math.floor(30 * (months % 1));
            return Math.floor(years) + ' years ' + Math.floor(months) + ' months ' + days + ' days';

        }

});

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

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

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

发布评论

评论列表(0)

  1. 暂无评论