I want to calculate in age of the person in days,months,year as of date May 2024. When the user selects the date of birth from the date picker days, months and year of the person has to be displayed in particular text boxes.
Year and month calculation works fine but days calculation is not working properly
Here is the code
<script type="text/javascript">
$(function () {
$("#XIStudentDOB").datepicker({
onSelect: function (value, ui) {
var toDate = new Date(2024, 05, 01);
var fromDate = new Date(value);
var days = (toDate - fromDate) / 1000 / 60 / 60 / 24;
var y = 365;
var y2 = 31;
var remainder = days % y;
var casio = remainder % y2;
year = (days - remainder) / y;
month = (remainder - casio) / y2;
day = (toDate - fromDate) / y;
var displayMonth = fromDate.getMonth()+1;
var DateString = fromDate.getDate() + "/" + displayMonth + '/' + fromDate.getFullYear();
$("#XIStudentDOB").val(DateString);
$("#age").val(year);
$("#months").val(month);
$("#days").val(day);
},
dateFormat: 'yy,mm,dd',
defaultDate: '2000,01,01',
maxDate: new Date((new Date).getFullYear(), 11, 31),
yearRange: '2000:2016',
changeMonth: true,
changeYear: true
});
});
<input type="text" name="XIStudentDOB" id="XIStudentDOB" class="form-control" />
<input type="text" name="yr" id="yr" maxlength="2" required="" />Year
<input type="text" name="month" id="month" maxlength="2" required="" />Months
<input type="text" name="days" id="days" maxlength="2" required="" />Days
How to calculate the days in from the date of birth using jquery
2
Answers
Create an HTML form with an input field for entering the date of
birth.
Use jQuery to capture the input date when the user submits the
form.
Calculate the difference between the current date and the
entered date of birth.
Display the calculated number of days.
$(document).ready(function() {
$(‘#dateForm’).submit(function(e) {
e.preventDefault();
Certainly! Here is a complete and corrected working code snippet for you
Here is an image example