Articles Archive
Articles Search
Director Wiki
 

Using Date Objects

June 14, 2000
by Gary Rosenzweig

Back in the pre-Director 7 days, developers complained about Lingo's poor handling of dates. You could get the computer's date by using the date, but it was returned in a format that depended on the user's system settings: "6/14/00" in the United States, and "14/6/00" in Europe, for instance. You could also use the long date to get strings like "Wednesday, June 14, 2000". In addition, it was not easy to extract useful data from the date. In you wanted to get the month, for instance, you would have to use string chunk expressions to extract it.

In Director 7 and 8, the date string expression still exists. However, it is there simply for backwards compatibility and for simple uses. A much more powerful feature now exists: date objects. Date objects are like points, rects, and colors in that they have a set of properties, and operations can be performed on them. The three visible properties of a date are year, month and day, in that order. Here is a simple example in the message window.

myDate = date(2000,6,14)
put myDate
-- date( 2000, 6, 14 )
put myDate.year
-- 2000
put myDate.month
-- 6
put myDate.day
-- 14

Using code like this you can create a date object from a set of numbers or variables and get the three properties of it easily, without dealing with string chunk expressions. However, there is one built-in date object that you can get: the systemDate. This returns the same date as seen with the date or the long date, but it does so as a date object.

put the systemDate
-- date( 2000, 6, 14 )
put (the systemDate).year
-- 2000
put (the systemDate).month
-- 6
put (the systemDate).day
-- 14

Not only are the elements of the systemDate easy to access, but they do not depend on the user's system settings. There is even a hidden property of the systemDate called the seconds that returns the number of seconds since midnight. With a little more Lingo work, you can use the systemDate as a substitute for the time as well. Keep in mind that the seconds is a new Director 8 addition to date objects.

put (the systemDate).seconds
-- 77566

It is amazing to me that very few Lingo programmers know about and use date objects. With what I have mentioned above, they are already more useful than the date. However, this is just the beginning. You can also perform operations on date objects. Say you wanted to find out tomorrow's date. Here is how to do it:

put the systemDate
-- date( 2000, 6, 14 )
put the systemDate + 1
-- date( 2000, 6, 15 )

So, big deal, right? How hard is it to add one to the day? Well, how about this:

myDate = date(2000,2,28)
put myDate
-- date( 2000, 2, 28 )
put myDate + 1
-- date( 2000, 2, 29 )
put myDate + 2
-- date( 2000, 3, 1 )

Not only do date objects know how to advance to the proper month, they even know about leap years. You can add or subtract any number of days from date objects and arrive at the property date. You can even subtract two dates to arrive at the number of days between them.

myBirthDate = date(1969,10,29)
today = the systemDate
put today - myBirthDate
-- 11186

Of course, the systemDate is still dependent on the user having set their computer to the proper date and time. Most applications, including email programs, rely on that too. You'll notice it when someone sends you email and it is dated years ago or years in the future. As you can see, date objects and the systemDate are far superior to the old the date system property. Use them to display the date and perform calculations and tricks.

Gary Rosenzweig's latest book is "Advanced Lingo for Games." In it, you can find the source code for more than 20 complete games. More information about the book can be found at http://clevermedia.com/resources/bookstore/book4.html. It can be purchased there, or in your local bookstore.

Gary Rosenzweig is the Chief Engineer, founder, and owner of CleverMedia, a game and multimedia development company in Denver, Colorado. He is the author of ten books on Macromedia Director and Flash, including his latest, Special Edition Using Director MX.

Copyright 1997-2024, Director Online. Article content copyright by respective authors.