| Register
Saturday, May 17, 2008   

Calculating Leap Years in Flash

Created By  Satori Canton, at  9/3/2004 - 15 comments.

Click to view this author's website.

Year 2004 is a leap year, with 29 days in February. We all generally know that leap years occur every four years, and most of the time, that is the case. However, there are some subtle but complex and important rules for calculating leap years.

In a recent project that I was working on, I was creating a custom calendar-type component for a rock band. They wanted a custom calendar that had a very specific look-and-feel and some unique functionality than the standard Flash Calendar Component does not provide.

Creating a Calendar Component from scratch isn't very difficult. The most complicated thing that I encountered was having to understand and calculate leap years, as users would page between various months or years on the calendar component.

Which Years are Leap Years?

In the Gregorian calendar, which is the calendar used by most modern countries, the following rules describe which years are leap years:

  1. Every year evenly divisible by 4 is PROBABLY a leap year

  2. The exception to rule #1 is that every year evenly divisible by 100 is NOT a leap year.

  3. The exception to rule #2 is that every year evenly divisible by 100 that is also evenly divisible by 400 IS a leap year.

This means that the years 1800, 1900, 2100, 2200, 2300 and 2500 are NOT leap years, while the years 2000 and 2400 are leap years. Though most of the talk when we went into the year 2000 was about Y2K, there was also some minor discussion that we were in a situation where all three exceptions applied making 2000, a year divisible by 100 normally not a leap year, was actually a leap year because it's also evenly divisible by 400.

It's kind of a confusing set of rules, and translating this into an ActionScript function that will tell you if a particular year is a leap year can be a little challenging. The verbose, but easier to read way of writing this function in ActionScript is as follows:

checkLeapYear = function (inYear) {
  if (inYear % 4 == 0) {
    if (inYear % 100 == 0) {
      return (inYear % 400 == 0);
    } else {
      return (true);
    }
  } else {
    return (false);
  }
};

If you use this function by passing a year into the function call, it will return a true (is a leap year) or false (is not a leap year) value.

If we step through the code, you can see that we start by testing if the year passed into the function (inYear) is evenly divisible by zero. The modulus operator (%) tells us if there is a remainder value after dividing by another value, in this case 4. By using '==0' after it, we are effectively evaluating the incoming variable to see if it's evenly divisible by the number 4. This like will come back as a true or false. If it's false, the function skips to the bottom 'else' statement and returns the value 'false' meaning that this year is not a leap year.

If however, line 2 'if(inYear %4 == 0)' evaluates out to true, we move to the next line down. This line uses the same modulus operator to see if the year is evenly divisible by 100. If it's not, it goes to the inner else statement and returns 'true' indicating that it is a leap year. Otherwise, it does a final test to see if the inYear value is evenly divisible by 400. If it is, it returns true (is a leap year), if it is not, it returns false (not a leap year). This line return (inYear % 400 == 0); does not use an if statement to return a true or false value because the == operator is going to return a Boolen value of true or false, make it simpler to write it as one line and allowing the result of true or false to be returned from the function.

This same function can actually be written on a single line of code. Because we are always working with Boolean values, we can use Boolean operators such as not (!) and (&&) and or (||) to avoid using any 'if' statements at all.

The simplified version of this formula would be written as:

checkLeapYear2 = function (inYear) {
  return ((inYear % 4 == 0) && !(inYear % 100 == 0)) || (inYear % 400 == 0);
};

You can test both functions with this code to see that they will result with the same result.

for(var i = 1880; i<2020; i++){
  trace(checkLeapYear (i) +" : "+ checkLeapYear2(i))
}

Need Professional Help For Your ActionScript Project?
ActionScript.com Consulting Services provide top quality professional ActionScript consulting to businesses around the globe. If you have a professional project in need to world-class talent, tell us about your project by requesting a quote today.

Reader Comments

  1. caseyc  Replied:
    ( 9/4/2004 At 2:37 AM)

    I've always left the hard work up to the Date object...

  2. eokyere  Replied:
    ( 9/4/2004 At 3:47 AM)

    http://blogs.okyere.org/resolve/archives/000039.htm

  3. Ash  Replied:
    ( 9/4/2004 At 3:06 PM)

    My crazy version:

  4. John Dalziel  Replied:
    ( 9/6/2004 At 4:58 PM)

    This link is a little old, but it shows you how to add some handy extra methods to the Date object using .prototype: http://www.ultrashock.com/tutorials/flash5/date_ext.html

  5. fadil  Replied:
    ( 9/11/2004 At 3:08 PM)

    hello,may i ask u.i have 1 problem.for now i have project to making report about production in my office n i want making with flash.what do u think ? n what program can i use to make thats programe online n always update with new report,thank's before

  6. John Daharsh  Replied:
    ( 11/14/2004 At 12:14 PM)

    I build a calendar from scratch a few years ago in Flash. I did all sorts of things that I ended up finding much simpler ways of doing later. The leap year issue is one of those.
    Basically, if you build your calendar using the Date object, you can instantate it using the year, or the year and the month, etc.
    Doing so takes away the need to know if its a leap year or not, since the date object for that year will automagically know if it's a leap year or not (the engineers of yore have probably built this into all OS's)
    Without typing a bunch of code I can't find, basically I created a new Date(year,month), then iterated over all the days in the month.
    The easiest way to know when the month had ended wasn't "30 days hath September blah blah blah" (which is why you would need to know when leap year is), it was to get day 0 of the following month (one day before day one) -- so to know if on a given year how many days are in February, you would create a new Date(year,2,0) -- and badabing -- there's how many days are in the month.
    I think the calendar may still be online here:
    http://www.deviantart.com/view/1449334/

  7. david  Replied:
    ( 11/24/2004 At 9:48 PM)

    it's sort of easy, but once you have a leap year friendly conversion from Date(); or an sql date stamp...hang on to it. I found the hard part was laying out the constants. like lowmonth=30, highmonth=31, noleap=28, yesleap=29, leapmonth=2. Once you establish the constants, then it's a couple arrays like, monthname, dayname, and this array numDaysInMonth=H,L,H,L,H,L,H,H,L,H,L,H (not syntax'd for space reasons)
    then...
    isLeapYear = (todaysYear%4) == 0 (true or false)
    a couple if statements and bingo bango boieee...you are on your way.

  8. No name, thanks  Replied:
    ( 11/30/2004 At 8:53 PM)

    Hi, great idea!
    This "day 0" idea has made my life a lot easier.
    Thanks again!

  9. Jerry Jasuta  Replied:
    ( 12/2/2004 At 2:02 PM)

    The date object is great, be some holidays are still hard to determine, Easter/Good Friday for example.
    Here is a bit of code that will determine most of the holidays celebrated in the USA
    http://jerryscript.hostrocket.com/flash/examples/holidays.html
    Example calendar using this code
    http://jerryscript.hostrocket.com/flash/calendar/

  10. ian  Replied:
    ( 2/26/2005 At 3:00 AM)

    Well there is a fourth rule.If the year is divisible by 4 and 100 and 400 and 1600 then it is not a leap year.Take an example of 3200 it would pass the first 3 tests but then what next .It is certainly not a leap year.But i would end up as a leap year.

  11. Xis  Replied:
    ( 2/28/2005 At 11:33 PM)

    não curti

  12. David  Replied:
    ( 3/1/2005 At 12:26 AM)

    I found all this info to work for me, the latest update's wont effect me as I will be dead by the time 3200 rolls around, hopefully the folks at Macromedia will have a real Date object by then. Once I built this "thing", I've cut and pasted it into all my date sensitive applications. And I have been busting them out one after another...thanks to all you contributors...

  13. Badger7  Replied:
    ( 3/17/2005 At 1:15 AM)

    Does this band plan on still being gigging in 95 years time? will they still have the same website in 5 years, let alone 95? I appreciate there are many other applications where this code is necessary, but this ain't one of them. Personally, I'm of the school of thought that any code that isn't required increases the risk of bugs for no benefit.

  14. carlos  Replied:
    ( 6/2/2005 At 11:10 AM)

    dear Mr.Satori Canton
    i study flash now, but i am can't understand how to built up a simple calendar with flash actionscript,
    and that,
    could you tell me where can i get the lebal tool in flashMX

    could you teach me above two problem?
    thank you for your help
    carlos

  15. Deba Prasad  Replied:
    ( 8/12/2006 At 7:54 AM)

    Dear sir,
    Im new to AcionScript in flash.As I am familiar with javaScript and core java itself,I dont hav much difficulties learning ActionScript.But I am very much confused of where to put the codes particularly to run a *.fla file.Could u plz forward me some ActionScript examples with explaning each step while putting he code in the Flash IDE.Im using Flash5.

    Regards,
    Deba Prasad Sharma

Login to post your comments. If you do not have an account with us please Register.
Copyright 2005 by ActionScript, Inc.   |  Privacy Statement  |  Terms Of Use  |  ActionScript Client Extranet