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.