7 Replies Latest reply on Mar 24, 2017 4:28 AM by beverly

    Days in month

    pharley

      Is there a simple way getting the number of days in a month eg. Get(daysinmonth;3) = 31   I suppose it would have to be year specific to account for leap years.  I know there is no specific Get function but what would be the simplest way to return this number?   Thanks

        • 1. Re: Days in month
          user19752

          As you wrote, you need year for getting the value.

          You can get it as "last day of the month"

           

          Day ( Date ( month + 1 ; 0 ; year ) )

          • 2. Re: Days in month
            beverly

            A really cool 'quirk' is the date format

            mm/0/yyyy

            that gives you the last day of the previous month

            AND.... FM does math very well, so

            Date(

            Month ( Get ( CurrentDate ) ) + 1 ;

            0 ;

            Year ( Get ( CurrentDate ) )

            )

            will give you the last day of THIS month. It will fix the leap days and years for you! Supply what ever date you want to know. If you add 1 to the month and then place 0 for the day, you will get the last day for that month & year.

             

            beverly

            • 3. Re: Days in month
              beverly

              And of course, this is the next step to just give you the number of days for the given month & year.

               

              beverly

              • 4. Re: Days in month
                ch0c0halic

                Day ( Date ( Month + 1 , 0 , Year )

                 

                FMP will take the 0 day of a month and make it the last day of the previous month.

                 

                So Mar = 3 add 1 = 4

                 

                Date ( 4, 0 , 2017 ) is the last day of the previous month or 3/31/2017.

                 

                Day of that date is 31, the last day of this month.

                • 5. Re: Days in month
                  pharley

                  Very neat. Thanks Beverly.  I didn't know the  month/0/Year thing.  Or maybe I forgot??  That will work well.

                  • 6. Re: Days in month
                    pharley

                    Thank you.  It's a simple solution.

                    • 7. Re: Days in month
                      beverly

                      So, if you think you will use this often, create a custom function:

                      /*

                        fn_GET_day0 ( dt ; numDays )

                        # return the last day of the month for a given date (dt)

                        when numDays = 0 or ""

                        # or

                        # return the number of days in a month for a given date (dt)

                        when numDays = 1 or "Y" or "Yes"

                      */

                      Let(

                        [ _m = Month ( dt )

                        // ; _d = Day ( dt ) // not used here

                        ; _y = Year ( dt )

                        ; _lastDayOfMonth = Date ( _m+1 ; 0 ; _y )

                        ; _result = Case

                        ( numDays ; Day ( _lastDayOfMonth )

                        ; _lastDayOfMonth

                        )

                        ]; _result

                      )

                      Then you can pass the date and a 'toggle' to show the last day of the month or the number of days in a month.

                      NOTE: supply the date format your system uses, of course!

                      fn_GET_day0(GetAsDate("24/3/2017");1) // number of days this month

                      fn_GET_day0(Get(CurrentDate);"") // last day of current month

                      beverly