1 2 Previous Next 16 Replies Latest reply on Apr 6, 2012 11:55 PM by AllegroDataSolutions

    Calculating Leap Year

    AllegroDataSolutions

      I am just wondering what other developers think is the best formula for calculating a leap year.

       

      Say, for example, you want to show a date that is 12 months later than the date the user enters. (Note that's 12 MONTHS, not 365 days later.)

       

      So, if the date entered is 3/31/2011, result would be 2/29/2012 and if he enters 3/31/2012, the result would be 2/28/2013.

        • 1. Re: Calculating Leap Year
          jbante

          How about Date ( Month ( inputDate ) ; Day ( inputDate ) ; Year ( inputDate ) + 1 ) - 1?

          • 2. Re: Calculating Leap Year
            johnenyedy

            Try: Set Variable [ $Days_per_year; Value: If ( Mod ( Year ( GetasDate ( $Accrue_to_Date )); 4 ) = 0; 366; 365 )]

            • 3. Re: Calculating Leap Year
              mikemccabe29

              I tried this one, but it didn't work:

               

              Date ( Month ( DateEntered ) ; Day ( DateEntered ) ; Year ( DateEntered ) + 1 ) - 1

               

              3/31/2011, result is 3/30/2012 rather than ....  2/29/2012

              3/31/2012, result is 3/30/2013 rather than .... 2/28/2013

              • 4. Re: Calculating Leap Year
                jbante

                I'm sorry. I got it wrong. It should have been Date ( Month ( inputDate ) ; 1 ; Year ( inputDate ) + 1 ) - 1.

                • 5. Re: Calculating Leap Year
                  Stephen Huston

                  Offering pseudo-code here to determine if year of the Date Entered is a leap year

                   

                  Case ( DayOfYear (Date ( 12 ; 31 ; Year( date entered ) ) = 365 ;

                  non-leap-year result ;

                  leap-year-result )

                   

                  The calcs from jbante above can be massaged to return the correct results once you know whether you're starting from a leap year.

                  • 6. Re: Calculating Leap Year
                    jbante

                    My corrected calc will work without having to branch based on whether or not there's a leap year involved; that complication is already handled by the Date () function. However, I was probably over hasty a second time over. My calc gives me the correct results for the dates specified, but what should the correct result be if the input date is 3/27/2011? 2/29/2012 or 2/27/2012?

                    • 7. Re: Calculating Leap Year
                      Stephen Huston

                      The  problem date, in my opinion, is what allegro wants to have returned if the date entered is 2/29/2012 or another leap Day itself.

                       

                      Allegro needs to let us know whether to return 2/28/2013 of 3/1/2013 since it was stated that we are looking for one year later, not 365 days later. So Leap Day is its own problem. Once we know the desired result for that date, the details of which way to jump in our calcs should be clearer.

                      • 8. Re: Calculating Leap Year
                        AllegroDataSolutions

                        As stated in my original post, "... if the date entered is 3/31/2011, result would be 2/29/2012 and if he enters 3/31/2012, the result would be 2/28/2013."

                         

                        If the calculations always count February as having 28 days, you will get a resut of March 1 on all years except leap year. What I am trying to do is to make the final day of Febrary as the 29th on leap year and 28 on all other years.

                         

                        The user enters a start date in a field, from which we use only the month and year. A field calculates the range of dates in that month (for example 1-31 for January). Other fields make the same calculation for the next 11 months. Since the starting month does not have to be January, the value for the year will not be the same in all date ranges.Therefore, the range in February must be either 1-28 or 1-29, depending on whether the year in which it falls is a leap year.

                         

                        What I wanted to do is simply put a calculation into the February calc to test whether the year is a leap year. But that makes the calcuations quite complicated. And, since, a lot of other calcs will use this information, the simpler I can make it, the better. That's why I was interested in seeing how other developers would handle it.

                        • 9. Re: Calculating Leap Year
                          RayCologon

                          allegro wrote:

                          ...What I am trying to do is to make the final day of Febrary as the 29th on leap year and 28 on all other years.

                           

                          Hi allegro,

                           

                          I believe the most straightforward way to do this is:

                           

                          Date(Month(StartDate); 0; Year(StartDate) + 1)

                           

                          ...which will always return the last day of the month preceding the anniversary of the StartDate, including when the next year is a leap year and the StartDate is in March.

                           

                          The general principle is similar to the method already suggested in a couple of Jeremy's posts, but the syntax is slightly simpler.

                           

                          Regards,

                          Ray

                          ------------------------------------------------

                          R J Cologon, Ph.D.

                          FileMaker Certified Developer

                          Author, FileMaker Pro 10 Bible

                          NightWing Enterprises, Melbourne, Australia

                          http://www.nightwingenterprises.com

                          ------------------------------------------------

                          • 10. Re: Calculating Leap Year
                            raybaudi

                            allegro ha scritto:

                             

                            Therefore, the range in February must be either 1-28 or 1-29, depending on whether the year in which it falls is a leap year.

                            Case(

                            Month ( Date ( 2 ; 29 ; year ) ) = 2 ; "1-29";

                            "1-28"

                            )

                            • 11. Re: Calculating Leap Year
                              RayCologon

                              raybaudi wrote:

                              Case(

                              Month ( Date ( 2 ; 29 ; year ) ) = 2 ; "1-29";

                              "1-28"

                              )

                               

                              Hi raybaudi,

                               

                              The problem with that approach is that allegro also said:

                               

                              allegro wrote:

                              What I wanted to do is simply put a calculation into the February calc to test whether the year is a leap year. But that makes the calcuations quite complicated. And, since, a lot of other calcs will use this information, the simpler I can make it, the better.

                               

                              - which seems to indicate that s/he already considered an approach of that kind - separately dealing with the results for February based on a calc expression that tests for a leap year - and rejected it, asking for other alternatives.

                               

                              Regards,

                              Ray

                              ------------------------------------------------

                              R J Cologon, Ph.D.

                              FileMaker Certified Developer

                              Author, FileMaker Pro 10 Bible

                              NightWing Enterprises, Melbourne, Australia

                              http://www.nightwingenterprises.com

                              ------------------------------------------------

                              • 12. Re: Calculating Leap Year
                                DrewTenenholz

                                Allegro --

                                 

                                Adding to what Ray wrote, the date calculation engine is quite capable:

                                 

                                Date ( month ; 0 ; year ) = the last day of the preceding month (thanks Ray!)

                                 

                                Date ( month ; day + 365 ; year ) = 365 days after the initial date 

                                (Think about this, for March 31, 2011, you are really putting Date ( 3 ; 396 ; 2011 ) into a formula which you could never type into a date field, but the date calculation engine just happily turns out the correct date)

                                 

                                Date ( month ; day ; year + 1 ) = the same date on the following year

                                (i.e. March 31, 2011 > March  31, 2012 which is 366 days later, and would be only 365 for 2012>2013.  Also, February 29, 2012 --> March 1, 2013.  Like it or not, that is the going to be the result. Compare with Date ( 2 ; 29 + 365 ; 2012 ) = February 28, 2013.)

                                 

                                 

                                Date ( month + 12 ; day ; year ) = the same date twelve months later

                                (This will do all manner of adjustments when the date is 29, 30, or 31.  Date ( 2 + 12 ; 29 ; 2012 ) = March 1 ; 2013 and Date ( 3 + 12 ; 31 ; 2011 ) = May 1 , 2012

                                 

                                 

                                It seems you have some fairly complicated date calculations.  My guess is you can simplify the entire process down to what Ray suggested without any exceptions, but you will know best.

                                 

                                -- Drew Tenenholz

                                • 13. Re: Calculating Leap Year
                                  Stephen Huston

                                  Hi Allegro,

                                   

                                  If the date entered is Leap Day -- 2/29/2012 -- which result do you want returned for one year later: 2/28/13 or 3/1/2013 ?

                                   

                                  That specific question still hasn't been answered.

                                  • 14. Re: Calculating Leap Year
                                    AllegroDataSolutions

                                    Sorry, I thought I did answer it. Or maybe I misunderstood. at I simplified the problem drastically to be able to discuss it here without getting bogged down in all the other stuff that depends on these calcs and might effect them. I've been working since 1:00 am and it's nearly 5:30 pm. I am a bit burned out, but I promise that I will re-read all your posts, try them out, and attempt to answer any unanswered questions.

                                     

                                    Some very nice work here. I think the simplest formula will probably be the best (the simplest ones usually are) and I really like making the day = 0 to get the last day of the preceeding month, because I never had the need for it before and didn't know that it was possible.

                                     

                                    Thanks, everyone.

                                     

                                    (Happy holiday, if it applies.)

                                    1 2 Previous Next