TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
How to Calculate Age using javaScript
Mukesh Kumar
Oct 16
2015
Code
1.2
k
0
1
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
function CalculateAge(birthday)
{
var re = /^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d+$/;
if
(birthday.value !=
''
)
{
if
(re.test(birthday.value))
{
birthdayDate =
new
Date(birthday.value);
dateNow =
new
Date();
var years = dateNow.getFullYear() - birthdayDate.getFullYear();
var months = dateNow.getMonth() - birthdayDate.getMonth();
var days = dateNow.getDate() - birthdayDate.getDate();
if
(isNaN(years)) {
document.getElementById(
'lblAge'
).innerHTML =
''
";
document.getElementById(
'lblError'
).innerHTML =
'Input date is incorrect!'
;
return
false
;
}
else
{
document.getElementById(
'lblError'
).innerHTML =
''
";
document.getElementById(
'lblAge'
).innerHTML = years +
' Years '
+ months +
' months '
+ days +
' days'
;
}
}
else
{
document.getElementById(
'lblError'
).innerHTML =
'Date must be mm/dd/yyyy format'
;
return
false
;
}
}
}
javascript
calculate age
C#