10.2. Retrieving the Day or Month Name
Problem
You want to retrieve the name of the day or the 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 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:
// Createdays
andmonths
arrays as properties of theDate
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 aDate
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( )]);
See Also
Get Actionscript Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.