Introduction
In this tutorial, I am going to show you how you can calculate the "difference between two dates in years" using javascript asp.net.
Javascript date difference in years |
Description
In this Example, I am taking asp.net textBox, if you have HTML textbox, the method of getting value from textbox will be different.
Find date difference using javascript with asp.net
<script type="text/javascript">
function daysdifference() {
var dob = document.getElementById('<%=txtdob.ClientID%>').value;
var doj = document.getElementById('<%=txtdoj.ClientID%>').value;
var date1 = new Date(doj);
var date2 = new Date(dob);
var y1 = date1.getFullYear();
var y2 = date2.getFullYear();
var diff = y1 - y2
if (diff > 18) {
}
else {
alert('Date of joining will be 18 year greater than DOB');
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td>Date Of Birth</td>
<td>
<asp:TextBox ID="txtdob" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Date of Joining</td>
<td>
<asp:TextBox ID="txtdoj" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID="btn" Text="check" runat="server" OnClientClick="daysdifference()" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
How to find date difference using javascript HTML
<head >
<title></title>
<script type="text/javascript">
function daysdifference() {
var dob = document.getElementById('txtdob').value;
var doj = document.getElementById('txtdoj').value;
var date1 = new Date(doj);
var date2 = new Date(dob);
var y1 = date1.getFullYear();
var y2 = date2.getFullYear();
var diff = y1 - y2
if (diff > 18) {
}
else {
alert('Date of joining will be 18 year greater than DOB');
}
}
</script>
</head>
<body>
<form >
<div>
<table>
<tr>
<td>Date Of Birth</td>
<td>
<input type="text" id="txtdob" />
</td>
</tr>
<tr>
<td>Date of Joining</td>
<td>
<input type="text" id="txtdoj" />
</td>
</tr>
<tr>
<td></td>
<td>
<button onclick="daysdifference()">Click me</button>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
Post a Comment