Please help with date algorithm

Posted on 2009-12-21
Last Modified: 2012-05-08
Given any date, I want to subtract days from that date until the date equals a "Sunday".

If executed on a Sunday, the from and to date range would be from Sunday to Sunday:

Sample method calls:

public class DateRange()
   public DateRange(){}

  public DateTime fromDate;
  public DateTime toDate;

  public void CreateRangeGivenDate(DateTime tempdt)

}//end of class

//calling code....

CreateDateRange cdr = new CreateDateRange();

//cdr.FromDate would now equal 12/20/2009
//cdr.ToDate would now equal 12/22/2009


//cdr.FromDate would now equal 12/20/2009
//cdr.ToDate would also equal 12/20/2009


//cdr.FromDate would now equal 12/20/2009
//cdr.ToDate would equal 12/26/2009
Question by:knowlton
    1 Comment
    LVL 15

    Accepted Solution

    Hi knowlton,
    try the below code.. this will work

    vb version

    Dim dtDate As Date
            Dim sFromdate As String
            Dim sToDate As String
            dtDate = Convert.ToDateTime(DateTextBox.Text)
            sToDate = dtDate.ToShortDateString()
            While dtDate.DayOfWeek <> DayOfWeek.Sunday
                dtDate = dtDate.AddDays(-1)
            End While
            sFromdate = dtDate.ToShortDateString()
            FromDateTextBox.Text = sFromdate

    c# version

        System.DateTime dtDate = default(System.DateTime);
        string sFromdate = null;
        string sToDate = null;
        dtDate = Convert.ToDateTime(DateTextBox.Text);
        sToDate = dtDate.ToShortDateString();
        while (dtDate.DayOfWeek != DayOfWeek.Sunday) {
            dtDate = dtDate.AddDays(-1);
        sFromdate = dtDate.ToShortDateString();
        FromDateTextBox.Text = sFromdate;

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    I have developed many web applications with asp & and to add and use a dropdownlist was always a very simple task, but with the new, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    745 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