Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. DayOfWeek/Date problem.

DayOfWeek/Date problem.

Scheduled Pinned Locked Moved C#
questionhelptutoriallounge
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Shady George
    wrote on last edited by
    #1

    This is a weird one from a client. I've been racking my brains on how to do this, but as yet, no success. Starting from a random date, to another random date (school term dates), how can I flag up the second Tuesday of each calendar month? I will accept hoots and jeers :laugh:

    L P M S 4 Replies Last reply
    0
    • S Shady George

      This is a weird one from a client. I've been racking my brains on how to do this, but as yet, no success. Starting from a random date, to another random date (school term dates), how can I flag up the second Tuesday of each calendar month? I will accept hoots and jeers :laugh:

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You could determine all the second Tuesdays for a year or two and then select the ones that fall within the target date range. Here are the second Tuesdays for 2014:

      for ( int i = 0; i < 12; i++ ) {

              DateTime beginDate = new DateTime( 2014, 01 + i, 01 );
      
              int dayDiff = ( int ) DayOfWeek.Tuesday - ( int ) beginDate.DayOfWeek;
      
              DateTime secondTuesday = beginDate.AddDays( dayDiff + ( dayDiff < 0 ? 14 : 7 ) );
      
              Console.WriteLine( "2nd Tuesday: {0}", secondTuesday );
      
           }  // end for.
      
      1 Reply Last reply
      0
      • S Shady George

        This is a weird one from a client. I've been racking my brains on how to do this, but as yet, no success. Starting from a random date, to another random date (school term dates), how can I flag up the second Tuesday of each calendar month? I will accept hoots and jeers :laugh:

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        Well, the brute force method is to find the first Tuesday of a month and add 7 days. No, I'm not trying to be funny here, here's a simple way to achieve this:

        public DateTime FindSecondTuesday(int month, int year)
        {
        int day = 1;
        DateTime date = new DateTime(year, month, 1);
        while (true)
        {
        if (date.DayOfWeek == DayOfWeek.Tuesday)
        return date.AddDays(7);
        date = date.AddDays(1);
        }
        }

        All you need do then, is call it for each month and year that you want to get the date from.

        Richard DeemingR 1 Reply Last reply
        0
        • S Shady George

          This is a weird one from a client. I've been racking my brains on how to do this, but as yet, no success. Starting from a random date, to another random date (school term dates), how can I flag up the second Tuesday of each calendar month? I will accept hoots and jeers :laugh:

          M Offline
          M Offline
          Mycroft Holmes
          wrote on last edited by
          #4

          I would take the essence of the other 2 solutions, apply ROW_NUMBER() partitioned over the date component and select the row number 2 for each month. Turn that into a view and select the record for a month/year

          Never underestimate the power of human stupidity RAH

          1 Reply Last reply
          0
          • P Pete OHanlon

            Well, the brute force method is to find the first Tuesday of a month and add 7 days. No, I'm not trying to be funny here, here's a simple way to achieve this:

            public DateTime FindSecondTuesday(int month, int year)
            {
            int day = 1;
            DateTime date = new DateTime(year, month, 1);
            while (true)
            {
            if (date.DayOfWeek == DayOfWeek.Tuesday)
            return date.AddDays(7);
            date = date.AddDays(1);
            }
            }

            All you need do then, is call it for each month and year that you want to get the date from.

            Richard DeemingR Online
            Richard DeemingR Online
            Richard Deeming
            wrote on last edited by
            #5

            No need for a loop - just switch on the DayOfWeek:

            public DateTime FindSecondTuesday(int month, int year)
            {
            DateTime date = new DateTime(year, month, 1);
            switch (date.DayOfWeek)
            {
            case DayOfWeek.Sunday:
            {
            return date.AddDays(9);
            }
            case DayOfWeek.Monday:
            {
            return date.AddDays(8);
            }
            case DayOfWeek.Tuesday:
            {
            return date.AddDays(7);
            }
            case DayOfWeek.Wednesday:
            {
            return date.AddDays(13);
            }
            case DayOfWeek.Thursday:
            {
            return date.AddDays(12);
            }
            case DayOfWeek.Friday:
            {
            return date.AddDays(11);
            }
            case DayOfWeek.Saturday:
            {
            return date.AddDays(10);
            }
            default:
            {
            throw new InvalidOperationException("Lousy Smarch weather!");
            }
            }
            }


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

            1 Reply Last reply
            0
            • S Shady George

              This is a weird one from a client. I've been racking my brains on how to do this, but as yet, no success. Starting from a random date, to another random date (school term dates), how can I flag up the second Tuesday of each calendar month? I will accept hoots and jeers :laugh:

              S Offline
              S Offline
              Shady George
              wrote on last edited by
              #6

              Thanks to everyone who answered my plea. - I've since found out that the meeting on the second Tuesday of the month is purely at one persons discretion, so I've decided to let that person enter the day on the timetable themselves. Thanks again, your answers were most appreciated.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups