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.

-Dman100-Software ConsultantAsked:
Who is Participating?
 
rajeeshmcaConnect With a Mentor Commented:
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-Software ConsultantAuthor Commented:
That is exactly what I needed.  I sincerely appreciate the help.  Thank you!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.