• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Lesson 28. Date Conversions > Setting Up a Date Comparison

Setting Up a Date Comparison

Now you will add some instructions so that clicking the buttons will actually compare dates. You saw earlier how to compare dates. Now you need to create a date variable that uses the year, month, and day that the user enters in the Year, Month, and Day fields. Just as you were able to set the text of the Today field to the current date, you can retrieve the existing text from a field.

1.
Open the Cast window. Click cast member 5 to select it and then click the Script button to create a cast member script.

Cast member 5 is the button that the user will click to compare today's date to the date entered. By default, Director displays the framework for an on mouseUp handler. This is what you need, because you want Director to calculate the date when the button is clicked.

2.
Modify the mouseUp handler as shown here in bold:

on mouseUp   --Get the values entered by the user.
							toYear = member ("To Year").text
							--Place the value in the "To Year" field in the variable toYear.
							toMonth = member ("To Month").text
							--Place the value in the "To Month" field in the variable toMonth.
							toDay = member ("To Day").text
							--Place the value in the "To Day" field in the variable toDay.
end

You have now assigned the text of each of the fields to three variables: toYear, toMonth, and toDay. This is still not quite what you want. You need to create a date value from this set of values so that the date value can be compared to the current date and the results can be displayed.

There are two ways to do this. You can create a date variable providing a string in the form "YYYYMMDD" (for instance, "19981231") by using Lingo's concatenation operator (&). The drawback to this approach is that if the user enters single digits for the single-digit months and days (for example, 1 and 2 instead of 01 and 02), then you could end up with a string like "199812", which isn't a valid date variable because it's not clear whether the month is 12 and the day is missing or the month is 1 and the day is 2. Valid date strings must have four digits for the year, followed by two each for the month and day—eight characters in all.

One way around this problem is to add a 0 to the beginning of each date if it is only one digit long. Another option, since the date variable allows you to enter numbers for dates in comma-separated format, date(YYYY,MM,DD), is to convert the dates to numbers and supply them that way. Separating the values with commas helps keep the month and day separate. Strings can be converted to numeric variables with the integer() function.

Note

The only way to retrieve the contents of a text or field member is through the text property. The member's contents are always stored in string format, which means that even a number like 9 is treated as a letter character "9" and not as a value on which you can perform calculations. Lingo provides conversion functions such as integer(), float(), value(), and string() that allow you to convert variables from internal string format to internal numeric format and back again.

3.
Modify the script of cast member 5 as shown here in bold:

on mouseUp     --Get the values entered by the user.
  toYear = member ("To Year").text
               --Place the value in the "To Year" field in the variable toYear.
  toMonth = member ("To Month").text
               --Place the value in the "To Month" field in the variable toMonth.
  toDay = member ("To Day").text
               --Place the value in the "To Day" field in the variable toDay.
               --Convert the variable's string content to integers.
							toYear = integer(toYear)
							toMonth = integer(toMonth)
							toDay = integer(toDay)
end


					  

Now each of the variables is converted to a number, like 1998, and is not a quoted string, like "1998". The difference is subtle but important to Lingo; it dramatically affects the way the information is treated. With the user's input saved as integers, you can now compare dates.

4.
Modify the script of cast member 5 as shown here in bold:

on mouseUp   --Get the values entered by the user.
  toYear = member ("To Year").text
             --Place the value in the "To Year" field in the variable toYear.
  toMonth = member ("To Month").text
             --Place the value in the "To Month" field in the variable toMonth.
  toDay = member ("To Day").text
             --Place the value in the "To Day" field in the variable toDay.
             --Convert the variable's string content to integers.
  toYear = integer(toYear)
  toMonth = integer(toMonth)
  toDay = integer(toDay)
  compareDate = date(toYear, toMonth, toDay)
							--Convert the user's input to date format and save the results in compareDate.
							difference = compareDate – the systemDate
							--Subtract compareDate from the current date and
							--save the results in difference.
end


					  

The first instruction builds a date variable from the year, month, and day components. The second instruction subtracts the current date and comes up with the difference between the target date and the current date. This is your objective. The only thing left to do is to update the Result1 text field with the answer.

5.
Enter the lines shown here in bold:

on mouseUp   --Get the values entered by the user.
  toYear = member ("To Year").text
             --Place the value in the "To Year" field in the variable toYear.
  toMonth = member ("To Month").text
             --Place the value in the "To Month" field in the variable toMonth.
  toDay = member ("To Day").text
             --Place the value in the "To Day" field in the variable toDay.
             --Convert the variable's string content to integers.
  toYear = integer(toYear)
  toMonth = integer(toMonth)
  toDay = integer(toDay)
  compareDate = date(toYear, toMonth, toDay)
             --Convert the user's input to date format and save the results in compareDate.
  difference = compareDate – the systemDate
             --Subtract compareDate from the current date and save the results in difference.
  member ("Result1").text = difference && "day(s)"
							--Display the result in the Result1 field.
end


					  

The double ampersand (&&) tells Director to create a string from the two components, placing a space in between. The result must be in string format to be reassigned to the text property, and the space in between allows the calculator to display, for instance, "42 day(s)" rather than "42day(s)". If you used just a single ampersand, Director would still have concatenated, or combined, the strings, but without inserting a space.

The handler now includes instructions to take the values from the three fields, build a date from these values, calculate the difference between the this date and the current date, and return the result in the text field. Try the calculator and see how it works.

6.
Close the Script Editor and play the movie. Enter values for the Year, Month, and Day fields. Then click the button that says "Find difference between today and this date."

The number of days is shown in the field to the right of the button.


Figure .


Congratulations! It works!

7.
Stop the movie and save your work.

The only task left is to wire up the other button so it will compare the two user-specified dates. The method will be similar.

8.
Open the Cast window and select the other button, cast member 6. Click the Script button to open the editor.

For the previous button, you added instructions to retrieve the user's entries from the fields on the screen and then you built a date variable from them. Then you compared this variable to the system date. For this button, which compares two arbitrary dates, you will need to retrieve information from both sets of fields and build two date variables. The procedure is nearly the same. In this case, the result will be displayed in Result2, not Result1.

9.
Enter the following mouseUp handler. This is a good place to use the cut-and-paste editing capabilities of the Script Editor.

on mouseUp    --Get the first date.
  toYear = member ("To Year").text
              --Place the value in the "To Year" field in the variable toYear.
  toMonth = member ("To Month").text
              --Place the value in the "To Month" field in the variable toMonth.
  toDay = member ("To Day").text
              --Place the value in the "To Day" field in the variable toDay.
              --Convert the variable's string content to integers.
  toYear = integer(toYear)
  toMonth = integer(toMonth)
  toDay = integer(toDay)
              --Get the second date.
  fromYear = member ("From Year").text
              --Place the value in the "From Year" field in the variable fromYear.
  fromMonth = member ("From Month").text
              --Place the value in the "From Month" field in the variable fromMonth.
  fromDay = member ("From Day").text
              --Place the value in the "From Day" field in the variable fromDay.
              --Convert the variable's string content to integers.
  fromYear = integer(fromYear)
  fromMonth = integer(fromMonth)
  fromDay = integer(fromDay)
              --Convert the user's input to date format and save.
  toDate = date(toYear, toMonth, toDay)
  fromDate = date(fromYear, fromMonth, fromDay)
  difference = fromDate – toDate
              --Subtract toDate from the fromDate and save the results in difference.
  member ("Result2").text = difference && "day(s)"
          --Display the result in the Result2 field.
end


					  

The instructions for getting the date from the first set of fields are essentially copied for the second set and the variable names changed. The two dates are then compared.

10.
Close the Script Editor and play the movie. Enter two dates and compare them by clicking the button labeled "Find difference between these two dates."

The difference between the two dates is displayed in the second result field.


Figure .


11.
Stop the movie and save your work.


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint