Recipe 10.2 Retrieving the Day or Month Name

10.2.1 Problem

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

10.2.2 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.

10.2.3 Discussion

The ActionScript Date class provides the getDay( ) and getMonth( ) methods, 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. The best way to do this is to define these arrays as properties of the Date class and store this information in a Date.as file that you can include in other projects:

// Create days and months arrays as properties of the Date class. Date.days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",               "Friday", "Saturday"]; Date.months = ["January", "February", "March", "April", "May", "June", "July",                "August", "September", "October", "November", "December"]; // Create a Date object for December 1, 2002, which is a Sunday. myDate = new Date(2002, 11, 1); // Displays: Sunday trace(Date.days[myDate.getDay(  )]); // Displays: December trace(Date.months[myDate.getMonth(  )]);

10.2.4 See Also

Recipe 10.3



ActionScript Cookbook
ActionScript 3.0 Cookbook: Solutions for Flash Platform and Flex Application Developers
ISBN: 0596526954
EAN: 2147483647
Year: 2005
Pages: 425

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