0

This is my HTML

<input type="text" class="dateofbirthpicker">
<input type="text" id="usia">

Script

$(function() {
    $('.dateofbirthpicker').datepicker({
        dateFormat: 'yyyy/mm/dd'
    });
});

window.onload=function() {
    $('.dateofbirthpicker').on('change', function() {
        var dob = new Date(this.value);
        var today = new Date();

        var age = Math.floor((today-dob) / (1000 * 60 * 60 * 24 * 360));
        ages = parseInt(age);
        $('#usia').val(ages);
    });
}

I have the above script to calculated age from date. It is returning age in Firefox but returns NaN in Chrome.

I was doing this Jquery Date.parse returning NaN in Chrome browser?

Result was invalid date when i ran the following...

var dob = new Date(this.value);
Community
  • 1
  • 1
TARA
  • 529
  • 1
  • 6
  • 23

2 Answers2

2

Change your dateformat as dateFormat: 'yy/mm/dd'

From specs,

  • y - year (two digit)
  • yy - year (four digit)

$(function() {
  $('.dateofbirthpicker').datepicker({
    dateFormat: 'yy/mm/dd',
    onSelect: function() {
      var dob = new Date(this.value);
      var today = new Date();
      var age = Math.floor((today - dob) / (1000 * 60 * 60 * 24 * 360));
      ages = parseInt(age);
      $('#usia').val(ages);
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="http://code.jquery.com/ui/1.8.18/jquery-ui.min.js"></script>
<link href="http://code.jquery.com/ui/1.8.21/themes/base/jquery-ui.css" rel="stylesheet" />
<input type="text" class="dateofbirthpicker">
<input type="text" id="usia">

JSFiddle Demo

Community
  • 1
  • 1
Rayon
  • 36,219
  • 4
  • 49
  • 76
  • yes i was try still can't var dob = new Date(this.value) ....... @rayon : invalid date.. i think its problem :(((((( – TARA Jun 27 '16 at 07:41
  • I'm just going to add a reason why, [for the date-picker a single `y` represents 2 digits](http://api.jqueryui.com/datepicker/#utility-formatDate) and not one. So `'yyyy/mm/dd'` isn't valid. – Spencer Wieczorek Jun 27 '16 at 07:41
  • @tara, Can you specify what is invalid ? I have provided working fiddle and snippet for you! You can highlight the issue over here.. – Rayon Jun 27 '16 at 07:42
  • @SpencerWieczorek, We edited by the same time probably or you were quicker by couple of seconds! Thanks anyways! ;) – Rayon Jun 27 '16 at 07:45
  • @tara, Could you reproduce the same in snippet I have provided ? – Rayon Jun 27 '16 at 07:48
  • yes it work.. but i can't do my mine .. work when select date 1-12.. but 13-30 its invalid date .. in mozilla working .. in chrome didnt work – TARA Jun 27 '16 at 07:50
  • i dont know .. because on mozilla working fine but in chrome can't – TARA Jun 27 '16 at 08:18
  • @tara, Did you change `dateFormat: 'yy/mm/dd'` ? – Rayon Jun 27 '16 at 08:21
  • @tara, Now what is the issue ? I have provided fiddle as well! – Rayon Jun 27 '16 at 08:25
  • but cant in mine .. wait .. im still search too the answer why.. cant in chrome – TARA Jun 27 '16 at 08:29
  • Rayon thanks for the discussion .. i get my answer ... thanks so much – TARA Jun 27 '16 at 08:43
0

Thanks all for discussion and suggest .. i get my answer, from here
New Date() returns invalid date

  $(function() {
    $('.dateofbirthpicker').datepicker({
            dateFormat: 'dd/mm/yy'
          });
  });
  window.onload=function(){
    $('.dateofbirthpicker').on('change', function() {
          var dselect = $(".dateofbirthpicker").val();
          var match = /(\d+)\/(\d+)\/(\d+)/.exec(dselect);
          var dob = new Date(match[3], match[2], match[1]);
          var today = new Date();

          var age = Math.floor((today-dob) / (1000 * 60 * 60 * 24 * 360));
      $('#usia').val(age);
    });
  }
Community
  • 1
  • 1
TARA
  • 529
  • 1
  • 6
  • 23
  • `dateFormat` decides the date value accessed using `.val()` method.. There is not need to test it through RegEx – Rayon Jun 27 '16 at 08:45
  • but .. if i do this --->>> var dob = new Date(this.value); --->> invalid date – TARA Jun 27 '16 at 08:59
  • 1
    Because you have not specified valid for =mat which is being accepted for `new Date` – Rayon Jun 27 '16 at 10:37