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

    Part I: Local Recipes
    Part II: Remote Recipes