Age Calculator
Class1.cs code
public class Class1
{
public class add
{
public static string Age(DateTime birthday)
{
DateTime now = DateTime.Today;
int age = now.Year - birthday.Year;
if (now < birthday.AddYears(age)) age--;
return age.ToString();
}
}
}
.aspx code
<html>
<body>
<table>
<tr>
<td>Enter your D O B</td>
<td><asp:TextBox ID="txtdate" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td></td><td><asp:Label ID="lbl2" runat="server"></asp:Label></td>
</tr>
<tr>
<td></td>
<td><asp:Button ID="btnsave" runat="server" Text="submit" onclick="btnsave_Click" /></td>
</tr>
</table>
</body>
</html>
aspx.cs code:
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnsave_Click(object sender, EventArgs e)
{
DateTime s = Convert.ToDateTime(txtdate.Text);
lbl2.Text= Class1.add.Age(s);
}
}
JavaScript Function for age calculator
<html>
<head>
<script type="text/javascript">
function ageCount() {
var date1 = new Date();
var dob= document.getElementById("dob").value;
var date2=new Date(dob);
var pattern = /^\d{1,2}\/\d{1,2}\/\d{4}$/; //Regex to validate date format (dd/mm/yyyy)
if (pattern.test(dob)) {
var y1 = date1.getFullYear(); //getting current year
var y2 = date2.getFullYear(); //getting dob year
var age = y1 - y2; //calculating age
document.write("Age : " + age);
return true;
} else {
alert("Invalid date format. Please Input in (dd/mm/yyyy) format!");
return false;
}
}
</script>
</head>
<body>
Date of Birth(dd/mm/yyyy):
<input type="text" name="dob" id="dob" />
<input type="submit" value="Age" onclick="ageCount();">
</body>
</html>
Class1.cs code
public class Class1
{
public class add
{
public static string Age(DateTime birthday)
{
DateTime now = DateTime.Today;
int age = now.Year - birthday.Year;
if (now < birthday.AddYears(age)) age--;
return age.ToString();
}
}
}
.aspx code
<html>
<body>
<table>
<tr>
<td>Enter your D O B</td>
<td><asp:TextBox ID="txtdate" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td></td><td><asp:Label ID="lbl2" runat="server"></asp:Label></td>
</tr>
<tr>
<td></td>
<td><asp:Button ID="btnsave" runat="server" Text="submit" onclick="btnsave_Click" /></td>
</tr>
</table>
</body>
</html>
aspx.cs code:
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnsave_Click(object sender, EventArgs e)
{
DateTime s = Convert.ToDateTime(txtdate.Text);
lbl2.Text= Class1.add.Age(s);
}
}
JavaScript Function for age calculator
<html>
<head>
<script type="text/javascript">
function ageCount() {
var date1 = new Date();
var dob= document.getElementById("dob").value;
var date2=new Date(dob);
var pattern = /^\d{1,2}\/\d{1,2}\/\d{4}$/; //Regex to validate date format (dd/mm/yyyy)
if (pattern.test(dob)) {
var y1 = date1.getFullYear(); //getting current year
var y2 = date2.getFullYear(); //getting dob year
var age = y1 - y2; //calculating age
document.write("Age : " + age);
return true;
} else {
alert("Invalid date format. Please Input in (dd/mm/yyyy) format!");
return false;
}
}
</script>
</head>
<body>
Date of Birth(dd/mm/yyyy):
<input type="text" name="dob" id="dob" />
<input type="submit" value="Age" onclick="ageCount();">
</body>
</html>
No comments:
Post a Comment