Monday, August 31, 2015

C# web window date calculator birtday check, date month year, two date differece checker


Get days difference between two given date. Total days



 ----.cs file code on button click
 protected void btndateDiff_Click(object sender, EventArgs e)
    {
        DateTime dtFrom = Convert.ToDateTime(txtFromDate.Text);
        DateTime dtTo = Convert.ToDateTime(txtToDate.Text);
        string res = (dtTo - dtFrom).TotalDays.ToString();
        lblMsg.Text = "<br /> Total Days = " + res;
        Age objA = new Age(dtFrom, dtTo);  /////////////age class
        lblMsg.Text += "<br /> Total--> Year: " + objA.Years + "/Months: " + objA.Months + "/Days: " + objA.Days;       
    }

/////////////age class

#region age calculation method
public class Age
{
    public int Years;
    public int Months;
    public int Days;

    public Age(DateTime Bday)
    {
        this.Count(Bday);
    }

    public Age(DateTime Bday, DateTime Cday)
    {
        this.Count(Bday, Cday);
    }

    public Age Count(DateTime Bday)
    {
        return this.Count(Bday, DateTime.Today);
    }

    public Age Count(DateTime Bday, DateTime Cday)
    {
        if ((Cday.Year - Bday.Year) > 0 ||
            (((Cday.Year - Bday.Year) == 0) && ((Bday.Month < Cday.Month) ||
              ((Bday.Month == Cday.Month) && (Bday.Day <= Cday.Day)))))
        {
            int DaysInBdayMonth = DateTime.DaysInMonth(Bday.Year, Bday.Month);
            int DaysRemain = Cday.Day + (DaysInBdayMonth - Bday.Day);

            if (Cday.Month > Bday.Month)
            {
                this.Years = Cday.Year - Bday.Year;
                this.Months = Cday.Month - (Bday.Month + 1) + Math.Abs(DaysRemain / DaysInBdayMonth);
                this.Days = (DaysRemain % DaysInBdayMonth + DaysInBdayMonth) % DaysInBdayMonth;
            }
            else if (Cday.Month == Bday.Month)
            {
                if (Cday.Day >= Bday.Day)
                {
                    this.Years = Cday.Year - Bday.Year;
                    this.Months = 0;
                    this.Days = Cday.Day - Bday.Day;
                }
                else
                {
                    this.Years = (Cday.Year - 1) - Bday.Year;
                    this.Months = 11;
                    this.Days = DateTime.DaysInMonth(Bday.Year, Bday.Month) - (Bday.Day - Cday.Day);
                }
            }
            else
            {
                this.Years = (Cday.Year - 1) - Bday.Year;
                this.Months = Cday.Month + (11 - Bday.Month) + Math.Abs(DaysRemain / DaysInBdayMonth);
                this.Days = (DaysRemain % DaysInBdayMonth + DaysInBdayMonth) % DaysInBdayMonth;
            }
        }
        else
        {
            throw new ArgumentException("Birthday date must be earlier than current date");
        }
        return this;
    }
}
#endregion

Asp code-------------------------
--------------------------------date calculation-------------
<br />
        From : <asp:TextBox ID="txtFromDate" runat="server"></asp:TextBox>[yyyy/mm/dd]<br />
        To : <asp:TextBox ID="txtToDate" runat="server"></asp:TextBox>[yyyy/mm/dd]<br />
        <asp:Button ID="btndateDiff" runat="server" Text="Diff" OnClick="btndateDiff_Click" /><br />

No comments:

Post a Comment