Get a date in relation to another in C#

Posted on 2011-10-11
Last Modified: 2012-05-12
I need a programmatic way to get the previous sunday and the following saturday dates when given a date, For example if I have the date today 11/10/2011 my code would return,

Sunday 09/10/2011
Saturday 15/10/2011
Question by:Blowfelt82
    LVL 53

    Accepted Solution

    Something like this...

    DateTime dtPrevSunday = DateTime.Now;
    while(dtPrevSunday.DayOfWeek != DayOfWeek.sunday) dtPrevSunday = dtPrevSunday.AddDays(-1);

    DateTime dtNextSaturday = DateTime.Now;
    while(dtNextSaturday.DayOfWeek != DayOfWeek.saturday) dtNextSaturday = dtNextSaturday.AddDays(-1);
    LVL 9

    Expert Comment


    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    In one of my recent projects, I was working with IP cameras, I need to take some pictures from the camera and do some processing on it. The first step, was to retrieve the image from camera into Image object. So that it can be displayed or …
    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    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

    18 Experts available now in Live!

    Get 1:1 Help Now