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
Calculate The Number Of Days, Months Or Years Between Two Dates Using Javascript
Mohd Arif
Jul 30
2016
Code
3.8
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
<html>
<head>
<title> Calculate The Number Of Days, Months Or Years Between Two Dates Using Javascript </title>
</head>
<body>
<div> Date of Birth <input type=
"text"
id=
"DOB"
name=
"DOB"
placeholder=
"dd/mm/yyyy "
></input> <br/> Current Date<input type=
"text"
name=
"Date"
id=
"Date"
placeholder=
"dd/mm/yyyy"
></input> <br/> <button type=
"Button"
onclick=
"age()"
>Calculate</button >
</div>
</body>
<script type=
"text/javascript"
>
function
age() {
debugger
;
var
birthdate = document.getElementById(
'DOB'
).value;
// in "dd/MM/yyyy" format
var
senddate = document.getElementById(
'Date'
).value;
// in "dd/MM/yyyy" format
var
x = birthdate.split(
"/"
);
var
y = senddate.split(
"/"
);
var
bdays = x[0];
var
bmonths = x[1];
var
byear = x[2];
//alert(bdays);
var
sdays = y[0];
var
smonths = y[1];
var
syear = y[2];
//alert(sdays);
if
(sdays < bdays) {
sdays = parseInt(sdays) + 30;
smonths = parseInt(smonths) - 1;
//alert(sdays);
var
fdays = sdays - bdays;
//alert(fdays);
}
else
{
var
fdays = sdays - bdays;
}
if
(smonths < bmonths) {
smonths = parseInt(smonths) + 12;
syear = syear - 1;
var
fmonths = smonths - bmonths;
}
else
{
var
fmonths = smonths - bmonths;
}
var
fyear = syear - byear;
alert(fyear +
' years '
+ fmonths +
' months '
+ fdays +
' days'
);
}
</script>
</html>
// This is just a sample script. Paste your real code (javascript or HTML) here.
if
(
'this_is'
==/an_example/){of_beautifier();}
else
{
var
a=b?(c%d):e[f];}
Calculate The Number Of Days
Months Or Years Between Two Dates
Javascript