6 Replies Latest reply on Apr 21, 2016 11:19 PM by justinc

    Days of month

    duncanbaker

      Hey folks

       

      I'm looking to find the best way of taking the current date and figuring out if it is the first Monday, first Tuesday, first Wednesday (carry on for rest of week), second Monday, second Tuesday (carry on), third Monday (carry on), fourth Monday (through Friday).

       

      I've got a first Monday custom function that I could modify to make into first Tuesday etc but this would mean a whole bunch of custom functions and something like a massive case statement to figure out if the current date equals the date calculated by all these.

       

      Maybe that's the way it needs to be, but thought I would throw it out there for other ideas.

       

      As always, many thanks.

        • 1. Re: Days of month
          erolst

          duncanbaker wrote:

          've got a first Monday custom function that I could modify to make into first Tuesday etc but this would mean a whole bunch of custom functions

          That's why functions can have arguments; you could modify the function to something like

           

          IsNthOccurrenceOfDayOfWeek ( dayOfWeek ; occurrence )

          • 2. Re: Days of month
            duncanbaker

            Mmm... So below is the CF (from BrianDunning site). Although I get your point, struggling to immediately think how this would be adjusted.

             

            /* ----------------------------------------------------------

            Returns the date for the first Monday in month

            'themonth' and year 'theyear'

             

            In:  themonth  - integer value 1 to 12 for the month

                   theyear - integer value for the year

                   if empty, the function uses the current date

             

            Return type: Date

            Syntax: FirstMonday ( month ; year )

             

            FirstMonday ( 5 ; 2005 )  returns May 2, 2005

             

            Author - Theo Ros

            ----------------------------------------------------------*/

             

            Let ( [ m = If ( IsEmpty ( themonth ) or themonth < 1 or themonth > 12 ;

                              Month ( Get ( CurrentDate ) ) ; themonth ) ;

                       y = If ( IsEmpty ( theyear ) ; Year ( Get ( CurrentDate ) ) ; theyear ) ] ;

              Date ( m ; Mod ( 2 - DayOfWeek ( Date ( m ; 1 ; y ) ) + 7 ; 7 ) + 1 ; y )

            )

            • 3. Re: Days of month
              okramis

              I'm looking to find the best way of taking the current date and figuring out if it is the first Monday, first Tuesday, first Wednesday (carry on for rest of week), second Monday, second Tuesday (carry on), third Monday (carry on), fourth Monday (through Friday).

              this should work:

               

              CF: NthDayOfWeek( _date )

               

              Let ( [

               

              _day = Day ( _date )

              ; _dayname = DayName ( _date )

              ; _nth = Div ( _day - 1 ; 7 )

              ; _nthstr =

              Case (

              _nth = 0 ; "first"

              ; _nth = 1 ; "second"

              ; _nth = 2 ; "third"

              ; _nth = 3 ; "fourth"

              ; _nth = 4 ; "fifth"

              ; "" )

               

              ] ;

               

              _nthstr & " " & _dayname

               

              )

               

              Regards

              Otmar

              • 4. Re: Days of month
                erolst

                Well, you can try this one; not thoroughly tested, though

                 

                /*-------------------------------------------------------------------------

                Signature: NthDayOfWeekInMonth ( theMonth ; theYear ; theDayOfWeek ; theOccurrence)

                Recursive: no

                Return type: date

                 

                Purpose: Returns the date for the nth occurrence of “theDayOfWeek”

                in month 'theMonth' and year 'theYear'

                 

                Arguments:  theMonth  - integer value 1 to 12 for the month

                                    theYear - integer value for the year

                                    (if empty, the function uses the current date)

                                    theDayOfWeek - integer value 1 to 7 for the day of the week

                                    theOccurrence – cannot possibly be larger than 5; could be checked before calculation commences

                 

                NthDayOfWeekInMonth ( 6 ; 2015 ; 5 ; 3 ) – the third Thursday in June 2015 – returns June 18th, 2015

                 

                Author: Oliver Stroh, from an original work of Theo Ros

                -------------------------------------------------------------------------*/

                 

                Let ( [

                  m =

                    Case (

                      theMonth < 1 or theMonth > 12 ;

                      Month ( Get ( CurrentDate ) ) ;

                      theMonth

                     ) ;

                  y = GetValue ( List ( theYear ; Year ( Get ( CurrentDate ) ) ) ; 1 ) ;

                 

                  dowFirst = DayOfWeek ( Date ( m ; 1 ; y ) ) ;

                  theDay = Mod ( theDayOfWeek - dowFirst + 7 ; 7 ) + 1 + ( ( theOccurrence - 1 ) * 7 ) ;

                 

                  result = Date ( m ; theDay ; y )

                  ] ;

                  Case ( Month ( result ) ≠ theMonth ; "ERROR" ; result )

                )

                • 5. Re: Days of month
                  duncanbaker

                  Thank you both very much. I'm going with okramis's solution as it gets me a result I can use with a little tweak: to return 1MO, 1TU etc. Appreciate the time taken to help out. 'Til next time!

                  • 6. Re: Days of month
                    justinc

                    Okramis, that is (almost) exactly the function that I was looking for.  The guts are exact, but  I'll be modifying the final output. 

                     

                    I needed something to calculate the "Nth Monday of the month", "Nth Tuesday..." etc.  Thanks!