Flex Date Utils – dayOfWeek
These methods are somewhat short so I’ll be going over dayOfWeek, and toFlexDayOfWeek
/** * Gets the day of the week * * @param date The date for which to get the day of the week * * @return A number representing the day of the week */ public static function dayOfWeek( date:Date ):Number { return date.getDay(); }
This one is perhaps not as useful as some of the others, as it can be called directly from the date object itself, but it is contained in the ColdFusion date functions, so I thought I would add it to the FlexDateUtils package, and for me, it is easier to remember DateUtils.dayOfWeek than date.getDay().
It accepts a date and then returns the day of the week as a number that that date represents. Valid return values would be 0 to 6.
To call this method, import the package:
import com.flexoop.utilities.dateutils.DateUtils;
and then:
private var _dow:Number = DateUtils.dayOfWeek( new Date( 2008, 4, 1 ) );
This would return 4 as May 1, 2008 ( dates in Flex also start from zero ) was a Thursday.
/** * Converts the day of the week to a Flex day of the week * * @param date The human readable day of week * * @return The Flex converted day of week or 0 aka Sunday */ public static function toFlexDayOfWeek( localDayOfWeek:Number ):Number { return ( localDayOfWeek > 0 && localDayOfWeek < 8 ) ? localDayOfWeek - 1 : 0; }
This method is really just a simplification of converting the day of the week from what most people think of, 1 to 7, to what Flex considers a day of the week, 0 to 6. I have another method that I will document in a later post that will convert the string version of a day of the week, to its numeric equivalent.
To call this method, import the package:
import com.flexoop.utilities.dateutils.DateUtils;
and then:
private var _flexDow:Number = DateUtils.toFlexDayOfWeek( 4 );
This would return 3 as 4 (Wednesday, starting from Sunday), would be 3 to Flex.
All methods in this series are describing code in the FlexDateUtils package I put on Google code.