• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 641
  • Last Modified:

calculate every sunday in the year

How would you calculate every sunday in a given year?  I want to display in a repeater the date of every sunday in a given year just by knowing the current date.

Thanks in advance for any help.

0
-Dman100-
Asked:
-Dman100-
1 Solution
 
rajeeshmcaCommented:
Hi Dman100,

Try the following
Aspx page

<div>
            <asp:Repeater ID="SundayRepeater" runat="server">
                <HeaderTemplate>
                    <table>
                        <tr>
                            <th>
                                Dates of Sunday in Current Year
                            </th>
                        </tr>
                </HeaderTemplate>
                <ItemTemplate>
                    <tr>
                        <td>
                            <asp:Label ID="DateLabel" runat="server" Text='<%# Bind("SunDate") %>'>
                            </asp:Label>
                        </td>
                    </tr>
                </ItemTemplate>
                <FooterTemplate>
                    </table>
                </FooterTemplate>
            </asp:Repeater>
        </div>

Code Behind (cs Page)

protected void Page_Load(object sender, EventArgs e)
    {
        DateTime dtCurrentDate;
        dtCurrentDate = DateTime.Now;
        DateTime dtStartDate = Convert.ToDateTime("01/01/" + dtCurrentDate.Year.ToString());
        DateTime dtEndDate =  Convert.ToDateTime("01/01/" + Convert.ToString(dtCurrentDate.Year + 1)).AddDays(-1);
        DataTable objDataTable = new DataTable();
        objDataTable.Columns.Add("SunDate");
        DataRow objDataRow;
        for (DateTime i = dtStartDate; i <= dtEndDate; i = i.AddDays(1))
        {
            if (i.DayOfWeek == DayOfWeek.Sunday)
            {
                objDataRow = objDataTable.NewRow();
                objDataRow["SunDate"] = i.ToShortDateString();
                objDataTable.Rows.Add(objDataRow);
                i = i.AddDays(6);
            }
        }
        SundayRepeater.DataSource = objDataTable;
        SundayRepeater.DataBind();
    }
0
 
-Dman100-Author Commented:
That is exactly what I needed.  I sincerely appreciate the help.  Thank you!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now