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 8342197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:42:00+00:00 2026-06-09T05:42:00+00:00

I have a what seems like simple date issue and I just can’t wrap

  • 0

I have a what seems like simple date issue and I just can’t wrap my head around trying to get it efficiently… I basically need to get the previous months date for a specific day.

For example: If today is the 3rd Thursday of the month, I want to get the 3rd Thursday’s date of last month. Its important that its based of the number of the day…ie: First Monday, 4th Friday, 2nd Wednesday, etc.

What’s the best way to get this done?

BTW…If there is not an equivalent previous months day that is fine. I can handle that. Also, currently I am counting the number or days (“Mondays”, “Tuesdays”, etc) manually to figure this out. I was just hoping there is a more elegant way to do it.

  • 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-06-09T05:42:01+00:00Added an answer on June 9, 2026 at 5:42 am

    Here’s what I would do:

    static DateTime? GetLastMonthSameNthDayOfWeek(DateTime date)
    {
        int nth = (date.Day-1) / 7; // returns 0 if 1st, 1 if 2nd...
        var prevMonthDay = date.AddMonths(-1);
    
        // find the first date of month having the same day of week
        var d = new DateTime(prevMonthDay.Year, prevMonthDay.Month, 1);
        while(d.Day <= 7)
        {
            if (d.DayOfWeek == date.DayOfWeek)
                break;
            d = d.AddDays(1);
        }
        // go to nth day of week
        d = d.AddDays(7 * nth);
        // if we have passed the current month, there's no nth day of week
        if (d.Month != prevMonthDay.Month)
            return null;
        return d;
    }
    

    Usage example:

    // 3rd wednesday of August 2012
    var a = new DateTime(2012, 8, 15);
    var aPrev = GetLastMonthSameNthDayOfWeek(a);
    // aPrev = July 18th 2012 (i.e. the 3rd wednesday of July 2012)
    
    // 5th wednesday of August 2012
    var b = new DateTime(2012, 8, 15);
    var bPrev = GetLastMonthSameNthDayOfWeek(b);
    // bPrev = null, because there's no 5th wednesday of July 2012
    

    N.B. :

    getting the ordinal position of the day of week inside a month is really easy:

    int nth = ((date.Day-1) / 7) + 1; // 1 -> 1st, 2 -> 2nd, 3 -> 3rd ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have what seems like a simple arrangement: a list of tags in a
I have what seems like a simple problem. I have a Spring web app,
This seems like a simple problem: I have a WF4 activity that guides the
This seems like such a simple question. I have several Edit boxes on my
This seems like it should be so simple, but apparently it isn't. I have
I have an issue that seems like very flaky behavour, is this a problem
I have looked at their help page it seems like I can register a
This seems like a simple exercise, but I'll be darned if I can find
It seems like I have begin ... rescue ... end statements everywhere in my
This seems like a must have form input for a mobile ui framework, but

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.