1

Am trying to add a date calculator on my web form using jquery using

Jquery Function

function AgeCalculator() {
  var dob1 = $('#datepicker1').val();
  var dob = new Date(dob1);
  var d = dob.getDate();
  var m = dob.getMonth();
  m += 1;  
  var y = dob.getFullYear();
  var dateofbirth= (d + "/" + m + "/" + y);
  dateofbirth = new Date(dateofbirth);
  var today = new Date();
  var age = Math.floor((today - dateofbirth) / (365.25 * 24 * 60 * 60 * 1000));
  $('#age').val(age);
}

If the day number exceeds 12, it's showing NaN for a date instead of 12+/mm/yy. I do not want to use any JQuery plugins.

Aju
  • 503
  • 1
  • 8
  • 26

1 Answers1

0

You need to add jquery ui plugin in your page.

$("#txtDate").val($.datepicker.formatDate('dd M yy', new Date()));

Here's the link to the official doc : (api.jqueryui.com/datepicker/#utility-formatDate)

Feras Al Sous
  • 1,073
  • 1
  • 12
  • 23