Wednesday, November 4, 2009

Date and Time

Date and Time
Flash provides the Date Class to handle the date and time. Creating a new instance of the Date Class generates an object with properties that contain the Year, Month, Date, and Time. This information is taken from the client operating system, so if the time or date is wrong on the system Flash is not responsible.

The Date class can also take parameters naming a year, month, date and time to generate a Date object for any date and time. This is useful when creating a calendar, since a calendar might display all of the days a in a month.

Using the Date Class in this way takes care of all of the troubles with calculating days in the month or leap years.


Get the day and time

To get the day and time make a new instance of the Date class with no parameters. The following lines of code:

var now:Date = new Date();
trace( now );

Should generate something like this in the output window.

Sat Oct 31 15:18:22 GMT-0700 2009

To get the individual elements like year, month, date, day and time, you can access the properties of the date instance you have created. For example:

var now:Date = new Date();
trace( now.month, now.date, now.fullYear );

This displays something like:

9 31 2009

Note: month is displayed as a number. Looking at the first example you can see it's Halloween when I wrote this, so the month should 10. Flash numbers the Months 0 to 11. Rather than 1 to 12. So you'll need to add one if you want to display the digit and expect everyone to understand it.

The same is true for the Date.day property. This property returns an integer between 0 and 6. where 0 is Mon and 1 is Tue etc.

To get a simple easy to read day month and year you can use Date.toLocaleDateString(). For example:

var now:Date = new Date();
trace( now.toLocaleDateString() );

This outputs something like:

Sat Oct 31 2009

Which is very readable. Here's a sampling of a few of the functions supplied by the Date class to produce the date and time in different formats:

  • Date.toLocaleDateString() - Sat Oct 31 2009
  • Date.toLocaleString() - Sat Oct 31 2009 03:34:36 PM
  • Date.toLocaleTimeString() - 03:34:36 PM
  • Date.toString() - Sat Oct 31 15:34:36 GMT-0700 2009
  • Date.toTimeString() - 15:34:36 GMT-0700
  • Date.toUTCString() - Sat Oct 31 22:34:36 2009 UTC

Very interesting.



Making a clock
To make clock with the Date class you'll need to create a new instance of the Date Class each time period that you want to update your clock.

For example if you want your clock to update once a minute, you would need to make a new instance of the Date class each minute.

if you want to make a clock that includes seconds, you'll need to make a new Date instance every second.

Use the Timer class to set up a timer event that occurs at your time period. For example the following creates a TextField that displays the time and updates every second. For more info on the Timer class see Timer Recipes.

var time_txt:TextField = new TextField();
time_txt.width = 200;
addChild( time_txt );

var timer:Timer = new Timer( 1000 );
timer.addEventListener( TimerEvent.TIMER, on_timer );
timer.start();

function on_timer( e:TimerEvent ):void {
    time_txt.text = new Date().toLocaleString();
}


Getting all the days of the current month
This would be the first step to building a calendar application. It could also be used in many other ways. The basic idea is to get the current month and year then make Date Objects using that month and year each with a different day.

If you make a Date Object with a day that is greater than the number of days in the current month the month will rollover to the next month. For example, imagine you make a Date Object for Feb 19 (and the year is not a leap year). In this case the Date object created will actually show the date as March 1.

The same happens with the year. Making a Date Object with a date of Dec 32 2009, the date of the Object would actually show as Jan 1 2010.

This recipe requires a loop to generate all of the days of the month. Be sure to read the recipes on loops. The following traces each day of the current month.

var now:Date = new Date();
var current_year:int = now.fullYear;
var current_month:int = now.month;
for ( var d:uint = 1; d < 32; d++ ) {
    var date:Date = new Date( current_year, current_month, d );
    if ( date.month == current_month ) {
        trace( date );
    }
}

The code above can be broken down into three parts.

The first, lines 1, 2 and 3, make a new Date object with the current date. Then we get the year and month and store them in the variables current_year and current_month for ease of use.


Second, we set up a for loop that will count from 1 to 31, using the variable d to hold the number of the count.

Last, with each loop we make a new Date object from the current_year and current_month, using d as the date. The idea is to make Date objects with the current years and month for the dates 1 to 31. Since not all months have 31 days, we check the month of each new Date to see if the month is different. For example November has only 30 days, so creating a Date Object with month 11 and day 31, will return a Date with Month 12 and date 1.
















No comments:

Post a Comment