Flex Date Utils – daysInMonth, dayOfWeekAsString
January 1st, 2009
These 2 methods are pretty self explanatory, daysInMonth and dayOfWeekAsString
/** * Gets the days in the month * * @param date The date to check * * @return The number of days in the month */ public static function daysInMonth( date:Date ):Number { // get the first day of the next month var _localDate:Date = new Date( date.fullYear, DateUtils.dateAdd( DateUtils.MONTH, 1, date ).month, 1 ); // subtract 1 day to get the last day of the requested month return DateUtils.dateAdd( DateUtils.DAY_OF_MONTH, -1, _localDate ).date; }
This method calculates how many days are in the month for the date passed in to the method. It takes the date parameter and sets a temporary date to the first day of the next month. Then it subtracts 1 day from that date to get the last day of the requested month.
So in order to use this method, first import the package
import com.flexoop.utilities.dateutils.DateUtils;
and then:
private var _total:Number = DateUtils.daysInMonth( new Date( 2009, DateUtils.monthAsNumber( DateUtils.JANUARY ), 1 ) );
This would return 31 to _total.
/** * Formats a date to the string version of the day of the week * * @param date The date to format * * @return A formatted day of week */ public static function dayOfWeekAsString( date:Date ):String { return DateUtils.dateFormat( date, "EEEE" ); }
This method returns a formatted date for the day of week. Nothing crazy here.
So in order to use this method, first import the package
import com.flexoop.utilities.dateutils.DateUtils;
and then:
private var _dow:String = DateUtils.dayOfWeekAsString( new Date( 2009, DateUtils.monthAsNumber( DateUtils.JANUARY ), 1 ) );
And this would return, Thursday
All methods in this series are describing code in the FlexDateUtils package I put on Google code.
Have you looked at DateFormatter? i.e. the following would do the same.
var dateFormatter:DateFormatter = new DateFormatter();
dateFormatter.formatString = “EEEE”;
trace( dateFormatter.format( new Date( 2009, 1, 1 ) ); // Thursday
Definitely. And that is actually what I am doing inside my “dateFormat” method. The dayOfWeekAsString is just a direct carryover from ColdFusion’s date library. For some reason they break out that functionality there, so I decided to include it here also. I didn’t really understand their reasoning behind including it, but I thought it wouldn’t hurt to include it in the library for those who didn’t want to mess with the dateFormat method directly. Thanks for reading though, and the suggestion.