Search This Blog

Monday, July 21, 2014

Calculate number of days between two days in c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string checkoutDate = "04/13/2014";//mm/dd/yyyy
        DateTime checkout = Convert.ToDateTime(checkoutDate);
        DateTime currentDate = DateTime.Now;
        Response.Write("<br>");
        Response.Write(currentDate);
        Response.Write("<br>");
        Response.Write(checkout);
        Response.Write("<br>");
        int dayscount = DateDiffInDays(currentDate,checkout);
        Response.Write("<br>");
       
       
        string script = "<script type='text/javascript'>alert('Search not allowed more then 365 days');</script>";
        if (dayscount > 365)
        {
            Response.Write(script);
        }
        else
        {
            Response.Write(dayscount);
        }
    }

    private int DateDiffInDays(DateTime currentDate, DateTime checkout)
    {

        return checkout.Subtract(currentDate).Days;

    }
}

1 comment :

Unknown said...

so nice article and useful to Dot Net learners. we are also providing Dot NEt online training our Cubtraining global leader in providing in Dot Net course.