Home > Flex, FlexDateUtils, code, library > Flex Date Utils – daysInMonth, dayOfWeekAsString

Flex Date Utils – daysInMonth, dayOfWeekAsString

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.

  1. January 3rd, 2009 at 05:30 | #1

    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

  2. Gareth
    January 3rd, 2009 at 11:10 | #2

    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.

  1. No trackbacks yet.