Friday, March 28, 2008

How To Find Sunday and Monday Between Two Dates







<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default13.aspx.cs"
Inherits="Default13" %>


<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="grd" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="Key" HeaderText="Date" />
<asp:BoundField DataField="Value" HeaderText="Date" />

</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>




using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Collections.Generic;

public partial class Default13 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DateTime StartingDate = DateTime.Parse("03/29/2008");
DateTime EndingDate = DateTime.Parse("04/30/2008");

Hashtable ht=new Hashtable();
foreach (DateTime date in GetDateRange(StartingDate, EndingDate))
{
if (date.DayOfWeek.ToString() == "Monday"
|| date.DayOfWeek.ToString()=="Sunday")
{

ht.Add(date.ToShortDateString(),date.DayOfWeek.ToString());
}
}
grd.DataSource=ht;
grd.DataBind();
}
private static List<DateTime> GetDateRange(DateTime StartingDate, DateTime EndingDate)
{
if (StartingDate > EndingDate)
{
return null;
}
List<DateTime> rv = new List<DateTime>();
DateTime tmpDate = StartingDate;
do
{
rv.Add(tmpDate);
tmpDate = tmpDate.AddDays(1);
} while (tmpDate <= EndingDate);
return rv;
}
}



No comments:

Post a Comment