Home > Flex, FlexDateUtils, code, library > Flex Date Utils – dayOfWeekIterationOfMonth

Flex Date Utils – dayOfWeekIterationOfMonth

January 2nd, 2009

This one is fairly complex, but quite useful, so I’m just going to demo one method today.

/**
 * Gets the Xth day of the month.
 * e.g. get the 3rd Wednesday of the month
 * 
 * @param iteration		The iteration of the month to get e.g. 4th or Last
 * @param strDayOfWeek	The day of the week as a string
 * @param date			The date containing the month and year
 * 
 * @return				The date of the xth dayOfWeek of the month 
 */
public static function dayOfWeekIterationOfMonth( iteration:Number, strDayOfWeek:String, date:Date ):Date {
	// get the numeric day of the week for the requested day
	var _dayOfWeek:Number = dayOfWeekAsNumber( strDayOfWeek );
	// get the date for the first of the month
	var _firstOfMonth:Date = new Date( date.fullYear, date.month, 1 );
	// calculate how many days to add to get to the requested day from the first of the month
	var _daysToAdd:Number = _dayOfWeek - DateUtils.dayOfWeek( _firstOfMonth );
	// if dayOfWeek is before the first of the month, get the dayOfWeek for the following week
	if ( _daysToAdd < 0 ) {
		_daysToAdd += 7;
	}
	// set the date to the first day of the week for the requested date
	var _firstDayOfWeekOfMonth:Date = DateUtils.dateAdd( DateUtils.DAY_OF_MONTH, _daysToAdd, _firstOfMonth );
	// return the date if iteration is 1
	if ( iteration == 1 ) {
		return _firstDayOfWeekOfMonth;
	} else {
		// if requesting an iteration that is more than is in that month or requesting the last day of week of month
		// return last date for that day of week of month
		if ( ( DateUtils.totalDayOfWeekInMonth( strDayOfWeek, date ) < iteration ) || ( iteration == DateUtils.LAST ) ) {
			iteration = DateUtils.totalDayOfWeekInMonth( strDayOfWeek, date );
		}
		// subtract 1 as it starts from the first dayOfWeek of month
		return DateUtils.dateAdd( DateUtils.WEEK, iteration - 1, _firstDayOfWeekOfMonth );
	}
}

The name is a little cryptic, but it returns the total number of a day of the week in a specified month. I heavily commented this method in order to be less confusing for anyone who might be trying to decipher what is going on, so I won’t go over what is being done within the method. The method accepts 3 parameters: iteration which represents which dayofweek for a month to get, 1, 2, 3, 4, 5, or LAST (if using the constant), the string representation of the day of the week, and the date containing the month and year to query. It returns the date for the specified iteration.

To implement, first import the package:

import com.flexoop.utilities.dateutils.DateUtils;

then:

private var _dateOfIteration:Date = DateUtils.dayOfWeekIterationOfMonth( 3, DateUtils.TUESDAY, new Date( 2008, DateUtils.monthAsNumber( DateUtils.MAY ) ) );

This will return May 20th, 2008, the 3rd Tuesday in May.

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

Gareth Flex, FlexDateUtils, code, library , ,

  1. tafkap
    December 24th, 2009 at 04:25 | #1

    Hi,

    thanks for your great job.

    Just one question, how can i get a date from day of year ?

    Cheers

  1. No trackbacks yet.