Skip to content Skip to sidebar Skip to footer

Given A Start Date , How To Calculate Number Of Years Till Current Date In Javascript

I have a start date value to be entered in a textbox in dd/mm/yyyy format, and as soon as value is entered in it, i want to fire onchange event for that textbox and and use current

Solution 1:

Here i got the solution

Calculate age in JavaScript

functiongetAge(dateString) {
    var today = newDate();
    var birthDate = newDate(dateString);
    var age = today.getFullYear() - birthDate.getFullYear();
    var m = today.getMonth() - birthDate.getMonth();
    if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
        age--;
    }
    return age;
}

Solution 2:

you cant with d/m/yyyy

d/m/yyy should be ISO compatible i.e : yyyy-mm-dd

var g="22/12/1978".split('/')

 newDate().getFullYear()- newDate(g[2]+"/"+g[1]+"/"+g[0]).getFullYear()

Solution 3:

You can parse the date manually and then use getFullYear() to calculate the difference. You can also use a library to parse the date, like jQuery UI

http://docs.jquery.com/UI/Datepicker/formatDate

Post a Comment for "Given A Start Date , How To Calculate Number Of Years Till Current Date In Javascript"