You need create asp .net website and add page that having name DateDiffe.aspx . You paste the below code according to page extension :-
1. ASPX PAGE CODE:-
<form id="form1" runat="server">
<div>
<table class="style1">
<tr>
<td>
Date 1</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Date 2</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="DateDiffBtn" runat="server" Text="Click"
onclick="DateDiffBtn_Click" />
</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</td>
</tr>
</table>
</div>
</form>
2. ASPX.CS PAGE CODE:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
public partial class DateDiffe : System.Web.UI.Page
{
private int[] monthDay = new int[12] { 31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
private DateTime fromDate;
private DateTime toDate;
private int year;
private int month;
private int day;
protected void Page_Load(object sender, EventArgs e)
{
TextBox2.Text = DateTime.Now.Date.ToString();
}
protected void DateDiffBtn_Click(object sender, EventArgs e)
{
DateTime dtDOB = new DateTime();
dtDOB = Convert.ToDateTime(TextBox1.Text);
int Year1 = dtDOB.Year;
int Month1 = dtDOB.Month;
int Date1 = dtDOB.Day;
int Year2 = DateTime.Now.Year;
int Month2 = DateTime.Now.Month;
int Date2 = DateTime.Now.Day;
int Month = 0;
int Year = 0;
int Date = 0;
fromDate = Convert.ToDateTime(TextBox1.Text);
toDate = Convert.ToDateTime(TextBox2.Text);
//Day Calculation
int increment = 0;
if (this.fromDate.Day > this.toDate.Day)
{
increment = this.monthDay[this.fromDate.Month - 1];
}
if (increment == -1)
{
if (DateTime.IsLeapYear(this.fromDate.Year))
{
increment = 29;
}
else
{
increment = 28;
}
}
if (increment != 0)
{
day = (this.toDate.Day + increment) - this.fromDate.Day;
increment = 1;
}
else
{
day = this.toDate.Day - this.fromDate.Day;
}
//Month Calculation
if ((this.fromDate.Month + increment) > this.toDate.Month)
{
this.month = (this.toDate.Month + 12) - (this.fromDate.Month + increment);
increment = 1;
}
else
{
this.month = (this.toDate.Month) - (this.fromDate.Month + increment);
increment = 0;
}
//Year Calculation
this.year = this.toDate.Year - (this.fromDate.Year + increment);
Label1.Text = year + "Year" + month + "Month" + day + "Day";
}
}
Happy coding.............................
Nice Post
ReplyDeleteThank You Keep reading
Delete