0

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 date to calculate number of years passed.

How can i do this in JavaScript, i have implemented in C#. is there any function in javascript which will help me calculate number of years between two dates?

Joe Doyle
  • 6,363
  • 3
  • 42
  • 45
Ishan
  • 4,008
  • 32
  • 90
  • 153

3 Answers3

1

Here i got the solution

Calculate age in JavaScript

function getAge(dateString) {
    var today = new Date();
    var birthDate = new Date(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;
}
Community
  • 1
  • 1
Ishan
  • 4,008
  • 32
  • 90
  • 153
0

you cant with d/m/yyyy

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

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

 new Date().getFullYear()- new Date(g[2]+"/"+g[1]+"/"+g[0]).getFullYear()
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
0

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

Willem D'Haeseleer
  • 19,661
  • 9
  • 66
  • 99