Home > ASP.Net > Get Date of Days between two Date

Get Date of Days between two Date


Hi all,

try this example to Get Date of Days between two Date

1) Create a website and add new web page


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default8.aspx.cs" Inherits="Default8" %>
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <table style="width:50%;">
        <tr>
            <td>
                From date :-
            </td>
            <td>
                <asp:TextBox ID="txt_FromDate" runat="server"></asp:TextBox>
                <cc1:CalendarExtender ID="txt_FromDate_CalendarExtender" runat="server"
                    Enabled="True" TargetControlID="txt_FromDate">
                </cc1:CalendarExtender>
            </td>
            <td>
               To date :-
            </td>
            <td>
                <asp:TextBox ID="txt_ToDate" runat="server"></asp:TextBox>
                <cc1:CalendarExtender ID="txt_ToDate_CalendarExtender" runat="server"
                    Enabled="True" TargetControlID="txt_ToDate">
                </cc1:CalendarExtender>
            </td>
        </tr>
        <tr>
            <td colspan="4">
                <asp:CheckBoxList ID="cbl_Days" runat="server" RepeatDirection="Horizontal">
                    <asp:ListItem>Saturday
                    </asp:ListItem>
                    <asp:ListItem>Sunday
                    </asp:ListItem>
                    <asp:ListItem>Monday
                    </asp:ListItem>
                    <asp:ListItem>Tuesday
                    </asp:ListItem>
                    <asp:ListItem>Wednesday
                    </asp:ListItem>
                    <asp:ListItem>Thursday
                    </asp:ListItem>
                    <asp:ListItem>Friday
                    </asp:ListItem>
                </asp:CheckBoxList>
            </td>
        </tr>
        <tr>
            <td colspan="4" align="center">
                <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
                Text="Get Data" />
            </td>
        </tr>
    </table>
    </form>
    </body>
</html>

and in code behind :-

protected void Button1_Click(object sender, EventArgs e)
    {
        //DateTime dFrom = new DateTime(2011, 1, 1);
        //DateTime dTo = new DateTime(2011, 1, 10);
        DateTime dFrom = DateTime.Parse(txt_FromDate.Text);
        DateTime dTo = DateTime.Parse(txt_ToDate.Text);
        //int diff = (dTo - dFrom).Days;
        TimeSpan DateDiff = dTo.Subtract(dFrom);
        for (int i = 0; i < DateDiff.Days; i++)
        {
            foreach (ListItem item in cbl_Days.Items)
            {
                if (item.Selected)
                {
                    if (dFrom.AddDays(i).DayOfWeek.ToString().ToLower() == item.Text.Trim().ToLower())
                    {
                        Response.Write(dFrom.Date.AddDays(i).ToString("MM/dd/yyyy")+" "+dFrom.Date.AddDays(i).DayOfWeek+"</br>");
                    }
                }
            }
        }
    }

Hope this helps

Good Luck

Advertisements
Categories: ASP.Net
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: