Recipe 14.3. Retrieving the Day or Month Name


Problem

You want to retrieve the name of the day or month.

Solution

Create arrays that contain the string values for the names of the days of the week and the names of the months of the year. Use the numeric day and month to extract the string values from the arrays.

Discussion

The ActionScript Date class provides the day and month properties, which return integer values representing the day of the week (from 0 to 6) and the month of the year (from 0 to 11). However, you may want the name of the day or month instead of its zero-relative number. To address this, create arrays containing the names of the days and months. Or, more conveniently, you can use constants of the custom ascb. util.DateFormat class. The constants DAYS, DAYS_ABBREVIATED, MONTHS, and MONTHS_ABBREVIATED are each arrays that contain strings that correspond to the numeric values returned by day and month. The actual definitions of those constants are as follows:

public static const DAYS:Array = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; public static const DAYSABBREVIATED:Array = ["Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat"]; public static const MONTHS:Array = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]; public static const MONTHSABBREVIATED:Array = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

You can then use those constants as shown in the following example:

var example:Date = new Date(2010, 3, 10); trace(DateFormat.DAYS[example.day]);  // Displays: Saturday trace(DateFormat.DAYSABBREVIATED[example.day]);  // Displays: Sat trace(DateFormat.MONTHS[example.month]);  // Displays: April trace(DateFormat.MONTHSABBREVIATED[example.month]);  // Displays: Apr

See Also

Recipe 14.4




ActionScript 3. 0 Cookbook
ActionScript 3.0 Cookbook: Solutions for Flash Platform and Flex Application Developers
ISBN: 0596526954
EAN: 2147483647
Year: 2007
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net