Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 387527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:40:55+00:00 2026-05-12T15:40:55+00:00

I’m creating a scheduler and need to be able to do the following in

  • 0

I’m creating a scheduler and need to be able to do the following in C#:

  1. Find the 1st Tuesday of June 2012
  2. Find the last Friday of March 2008
  3. Find every Saturday in January 2013
  4. Find the 3rd Friday in July 2009
  5. Find every Saturday over the next 3 months
  6. Find every day in March 2018

The results should come back as DateTime or List<DateTime>.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T15:40:55+00:00Added an answer on May 12, 2026 at 3:40 pm

    Here are methods to find the first/last specified day of week in a given month:

    public DateTime GetFirstDayOfWeekInMonth(int year, int month, DayOfWeek dayOfWeek)
    {
        DateTime dt = new DateTime(year, month, 1);
        int first = (int)dt.DayOfWeek;
        int wanted = (int)dayOfWeek;
        if (wanted < first)
            wanted += 7;
        return dt.AddDays(wanted - first);
    }
    
    public DateTime GetLastDayOfWeekInMonth(int year, int month, DayOfWeek dayOfWeek)
    {
        int daysInMonth = CultureInfo.CurrentCulture.Calendar.GetDaysInMonth(year, month);
        DateTime dt = new DateTime(year, month, daysInMonth);
        int last = (int)dt.DayOfWeek;
        int wanted = (int)dayOfWeek;
        if (wanted > last)
            last += 7;
        return dt.AddDays(wanted - last);
    }
    

    From those, you can easily find the answers to the other questions… just add 7 days to find the next occurence of the day you’re looking for


    EDIT: thinking more about it, it would be pretty handy to have that in the form of extension methods, such as :

    Console.WriteLine("Next monday : {0}", DateTime.Today.Next(DayOfWeek.Monday));
    Console.WriteLine("Last saturday : {0}", DateTime.Today.Previous(DayOfWeek.Saturday));
    

    Here is the implementation :

    public static class DateExtensions
    {
        public static DateTime Next(this DateTime from, DayOfWeek dayOfWeek)
        {
            int start = (int)from.DayOfWeek;
            int wanted = (int)dayOfWeek;
            if (wanted < start)
                wanted += 7;
            return from.AddDays(wanted - start);
        }
    
        public static DateTime Previous(this DateTime from, DayOfWeek dayOfWeek)
        {
            int end = (int)from.DayOfWeek;
            int wanted = (int)dayOfWeek;
            if (wanted > end)
                end += 7;
            return from.AddDays(wanted - end);
        }
    }
    

    It’s probably more flexible than the first methods I suggested… With that, you can easily do things like that :

    // Print all Sundays between 2009/01/01 and 2009/03/31
    DateTime from = new DateTime(2009, 1, 1);
    DateTime to = new DateTime(2009, 3, 31);
    DateTime sunday = from.Next(DayOfWeek.Sunday);
    while(sunday <= to)
    {
       Console.WriteLine(sunday);
       sunday = sunday.AddDays(7);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Seemingly simple, but I cannot find anything relevant on the web. What is the
I need a function that will clean a strings' special characters. I do NOT
I have thousands of HTML files to process using Groovy/Java and I need to
I need to clean up various Word 'smart' characters in user input, including but
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.