calculate every sunday in the year

Posted on 2009-12-21
Last Modified: 2012-06-27
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.

Question by:-Dman100-
    LVL 10

    Expert Comment

    LVL 15

    Accepted Solution

    Hi Dman100,

    Try the following
    Aspx page

                <asp:Repeater ID="SundayRepeater" runat="server">
                                    Dates of Sunday in Current Year
                                <asp:Label ID="DateLabel" runat="server" Text='<%# Bind("SunDate") %>'>

    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();
            DataRow objDataRow;
            for (DateTime i = dtStartDate; i <= dtEndDate; i = i.AddDays(1))
                if (i.DayOfWeek == DayOfWeek.Sunday)
                    objDataRow = objDataTable.NewRow();
                    objDataRow["SunDate"] = i.ToShortDateString();
                    i = i.AddDays(6);
            SundayRepeater.DataSource = objDataTable;

    Author Closing Comment

    That is exactly what I needed.  I sincerely appreciate the help.  Thank you!

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now