Home > Flex, FlexDateUtils, code, library > Flex Date Utils – daysInYear, isLeapYear

Flex Date Utils – daysInYear, isLeapYear

These are the final 2 methods in my DateUtils Class of the FlexDateUtils library. I’ve got a Business class, a Holiday class and a DaylightSavingTimeUS class that are all included in the FlexDateUtils library, and I may go over those in the future, but not right now.

/**
 * Gets the number of days in the year
 * 
 * @param date	The date to check
 * 
 * @return		The total number of days in the year
 */
public static function daysInYear( date:Date ):Number {
	return DateUtils.dateDiff(
				DateUtils.DAY_OF_MONTH,
				new Date( date.fullYear, DateUtils.monthAsNumber( DateUtils.JANUARY ), 1 ),
				DateUtils.dateAdd( DateUtils.YEAR, 1, new Date( date.fullYear, DateUtils.monthAsNumber( DateUtils.JANUARY ), 1 ) ) );
}

Very straight forward method. This calculates how many days are in the year that is passed in. Essentially I’m doing a date difference between the first of January of the requested year, and the first of January of the next year. The method returns the number of days for that year.

So in order to use this method, first import the package

import com.flexoop.utilities.dateutils.DateUtils;

and then:

private var _total:Number = DateUtils.daysInYear( new Date( 2009, DateUtils.monthAsNumber( DateUtils.JANUARY ) ) );

This would return 365.

/**
 * Determines whether the year is a leap year or not
 * 
 * @param date	The date to check
 * 
 * @return		<code>true</code> means it is a leap year, <code>false</code> means it is not a leap year.
 */
public static function isLeapYear( date:Date ):Boolean {
	return daysInYear( date ) > 365;
}

Another very straight forward method. It accepts a date and determines if the year is a leap year.

So in order to use this method, first import the package

import com.flexoop.utilities.dateutils.DateUtils;

and then:

private var _isLeapYear:Boolean = DateUtils.isLeapYear( new Date( 2009, DateUtils.monthAsNumber( DateUtils.JANUARY ) ) );

This would return false as 2009 is not a leap year.

All methods in this series are describing code in the FlexDateUtils package I put on Google code.

I hope that this wasn’t too dry a topic for everyone that followed along. I just get excited when I’m describing code I have written and as much as my wife looks like she’s interested in what I’m saying, I see her eyes glazing over every now and then :) , and I hope that others find new code as interesting as I do. I wanted to make sure I got descriptions of everything in the DateUtils library to make it easier for anyone trying to figure out a use for it. I think I’ll leave the library descriptions alone and go back to posting solutions to problems I’ve faced at work/home or interesting tidbits I’ve come across. Perhaps I can revisit any new items I add in the future.

  1. January 7th, 2009 at 13:40 | #1

    Gareth, great work on this! DateTime is coming along quickly, and I’m excited for us to merge our libraries together.

  2. Gareth
    January 7th, 2009 at 13:47 | #2

    Thanks, Nate. I was just wondering how your DateTime/DST stuff was coming along. Should make for a nice, full-featured package once it’s done. Can’t wait :)

  3. Dean
    February 19th, 2009 at 09:55 | #3

    Gareth,
    Thanks for your utilities. First, have you updated the Google Code with the latest items posted for the date utilities?

    Second. When I as looking for the Week of Year, I didn’t notice that you already had something in the Date Utilities. Maybe you just added it and hadn’t put it on the Google Code. I was wondering if your method handles the issues discussed here (http://www.actionscript.org/forums/showthread.php3?t=65502). I used the code from this forum and converted it to AS3 code to work for my application.

    Third. I probably didn’t do this the right way, but I need to convert a date with a month abbreviation. So, I just modified your code to have an array for abbreviated months and then created a function to get the month number from the abbreviation.

    Again, thanks for your code.

  4. Gareth
    February 19th, 2009 at 22:00 | #4

    Hey Dean,
    No problem. Glad that they’re useful to you. On to your questions:

    1. Yup, everything that I’ve documented is in the google code base (and a few things are in there that I haven’t yet blogged on such as Holiday and the DST stuff)
    2. Yes, weekOfYear is a little over halfway through DateUtils.as (line 303 according the the “view source” at googlecode). The method accepts a date and returns the week of the year (from 1 to 53). Here’s the blog post
    3. I’m not sure there’s a right or a wrong way to do it, but that certainly sounds like a logical method of solving the problem :) You could also perhaps try Date.parse( dateAsString ) on the date, but that can also return null (if the date isn’t formatted correctly), so your way seems best.

    Gareth

  5. Phillip
    April 14th, 2009 at 05:40 | #5

    Hi Gareth,

    great work on this, i like how close you are to the CF Functions! I read, u updated the code on google, but when i look into the 1.0.4 release, all .as classes are of date 22.12.2008.

    maybe you find the time to commit your latest version.

  6. Gareth
    April 14th, 2009 at 14:23 | #6

    I checked all of my local files and December 22, 2008 was the last time I edited the files, so what’s up there should be the latest version.

    Thanks for checking out the library.

  7. Tom
    June 9th, 2009 at 04:08 | #7

    Just downloaded your sources, which sadly don’t work for me. Your implementation for weekOfYear is too simplistic. You can’t just divide the day of the year by 7 and return that as the week.

    A proper implementation would use something like a ‘minimal-days-in-first-week’ value (which is four in Germany) to figure out when the first week of the year starts, and when the last week ends.

    For example, the 29-Dec-2008 is in week 01/2009, and the 29-Dec-2009 is in week 53/2009. Note the year difference in the first example.

    Cheers
    Tom

  8. Gareth
    June 10th, 2009 at 14:43 | #8

    Actually, there is a standard for this which I did not know about. After doing a little more research, I will be updating this to use the standard (which appears to be documented very well here). Thanks for bringing this to my attention. I’ll make an blog update when I’ve made the changes.

  9. Junaid Abbasi
    June 15th, 2009 at 23:05 | #9

    Hi Gareth,

    In your DateUtils.dateDiff(“date”, startDate, endDate) method, it returns -1 if startDate has a time component > endDate’s time component. It should return 0 if startDate is greater by < 24 hrs.

    -Junaid.

  10. Gareth
    August 26th, 2009 at 15:04 | #10

    @Tom,
    I have updated the weekOfYear method so it now returns the correct weekOfYear (using the ISO standard). I’ve also added unit tests to the library, so any changes I may make should be caught before they’re delivered out.

  1. January 9th, 2010 at 14:32 | #1