Pin Me

Returning a Date to a DateField Component in ActionScript

written by: S. R. Obbayi•edited by: Amy Carson•updated: 2/25/2011

If you ever had difficulties returning a date to a DateField component in ActionScript then you are in the right place. In this tutorial you will learn not only how to do it but also how to avoid the most common mistakes encountered when handling DateFields.

  • slide 1 of 6

    Introduction

    Flash datefield component Before we dive into returning a date to a DateField component in ActionScript, we will need to know this tutorial is based on Flash CS4 and uses ActionScript 2.0. You will also need to be familiar with the Flash CS4 user interface as this tutorial assumes that you are. There are other similar Flash tutorials such as this one on creating a Flash contact form that will give you more information on how to layout your Flash web forms.

  • slide 2 of 6

    Setting Up Your Scene

    Flash CS4 Timeline Our scene consists of three Flash components laid out vertically. The components include a DateField, a Button and a TextInput. The Button and the TextInput are basically there to provide interactivity and for unit testing.

    All the Flash components are placed in a single layer on the TimeLine with an additional layer named “actions". We then place the actions we need on the first layer of the actions layer. This is sufficient for this tutorial. Other tutorials may need more complex layouts. Here is a tutorial on making a Flash dice roller. Here you can see a different set up of the TimeLine.

  • slide 3 of 6

    Our DateField Related ActionScript

    The code for returning a date to a DateField component in ActionScript consists of only three lines of code as follows:

    date_field.selectedDate = new Date(2011, 0, 25);

    date_button.onRelease = function() {

    message_date.text = date_field.selectedDate;};

    In the code above the labels above date_field, date_button and message_date are user defined identifiers for each of the three Flash form components.

    The first line in the ActionScript code above returns a date to the DateField component. That single line is all that is needed to get the job done. The next two lines are simply there for unit testing and to make sure that everything is working as expected by returning the date from the DateDield component.

    The second line catches the Click event from the Button and then the third line reads the date that was selected and displays it in a TextField.

  • slide 4 of 6

    Explaining The Date() Class

    Flash datefield results The Date() Class is where all the behind the scenes logic work. The Date() Class represents date and time information and takes parameters that can represent a time value, a date in the form of year, month and date. the month here is a numeric value from Zero to 11 with Zero representing January. Additionally you can have hours, minutes, seconds and milliseconds.

    The example used here formats a date with the year as 2011, the month as January rememing zero represents the first month and the 25 is the day of the month.

  • slide 5 of 6

    Things to Look out For

    Make sure you do not enter the date as a string of characters as this will give you an error. Some people tend to to use the .text method to read DateField Components in Flash. This will not work. Remember to enter The dates in valid formats. For example there are 60 seconds in a minute and 60 minutes in an hour. Some people may accidentally count these up to 99. There you have it. The simplest of returning a date to a DateField component in ActionScript.

    You can get a copy of the Flash Source File here to see this example in action.

  • slide 6 of 6

    References

    Source: author’s own experience.

    All screenshots from the author's own computers.