Articles Archive
Articles Search
Director Wiki

Using Date Objects and HTML to Make Calendars

June 28, 2000
by Gary Rosenzweig

Two weeks ago, we looked at the use of date objects and the systemDate to easily and accurately determine the date and perform operations with dates. A natural extension of this is to be able to make calendars in Director.

With the systemDate, you can determine today's date. Or, you can create a date object for any day. A great feature of date objects is that you can perform operations on them. For instance, you can subtract the first of the month from January 1, 1970. The result will be the number of days since January 1, 1970. Take this number, divide by 7, and the remainder will tell you the day of the week, as it relates to January 1, 1970, which was a Thursday. Here is an example:

-- Welcome to Director --
d = date( 2000, 6, 1 )
put d - date(1970,1,1)
-- 11109
put 11109 mod 7
-- 0

The remainder is 0, which means that June 1 of this year is a Thursday. Had the remainder been a 1, then the date would have corresponded to a Friday. The ability to determine which dates fall on which days of the week is crucial if you are to make calendars.

To create a monthly calendar, you also need to know how many days are in a month. This can also be done with a calculation performed on a date object. For instance, to see if the 30th is the last day of June, all you need to do is add 1 to the 30th of June and see if the months are equal. If they aren't, then the 30th is the final day of the month.

d = date(2000,6,30)
testdate = d+1
put d.month
-- 6
put testdate.month
-- 7

Now that you know that you can correctly place dates on days of the week and notice when the number of days in the month run out, you can make a monthly calendar. A convenient way to do this is to use HTML tables. This will place the dates in cells and allow the dates to be displayed in a familiar format. Here is the resulting handler.

on makeCalendar

  -- get today and reset day to the first of the month
  d = the systemDate = 1

  -- figure out days of week based on Jan 1, 1970 being a Thursday
  dotw = (d - date(1970,1,1) + 4) mod 7

  -- start HTML

  -- put the month and year at the top
  list = ["January","February","March","April","May","June","July","August","September","October","November","December"]
  month = list[d.month]
  put "<B>"&month&&d.year&"</B>" after html

  -- start the table
  put "<TABLE BORDER=1 CELLPADDING=3><TR>" after html

  -- fill in blank cells until first day of month
  repeat with i = 1 to dotw
    put "<TD></TD>" after html
  end repeat

  -- loop through all days of month
  repeat with i = 0 to 30
    -- add the cell with the date
    put "<TD>"&(i+1)&"</TD>" after html
    -- see if tomorrow is same month
    testdate = d+1
    if testdate.month <> d.month then
      -- end the calendar in the middle of the week
      put "</TR>" after html
      exit repeat
      -- see if tomorrow is the start of a new week
    else if ((dotw+1) mod 7) = 0 then
      -- end a row of the calendar
      put "</TR>"&RETURN after html
      -- make sure there is a new week this month before adding new row
      if testdate.month = d.month then put "<TR>" after html
    end if
    -- advance the day
    d = d + 1
    dotw = dotw + 1
  end repeat

  -- finish row
  put "</TABLE></CENTER></BODY></HTML>" after html

  -- set html of text member
  member("calendar").html = html


A Director 8 sample movie is available for download in Mac or PC format.

The inclusion of the month and year are just nice cosmetic touches. If you want to be able to show next month's calendar, just add 1 to the month property of the original date. Be careful, though. If the month is 12, and you try to add 1 to it, you will get an error message. Instead, change the month to 1 and add 1 to the year.

A lot can be done to fancy this up. Instead of using boring table borders and html default fonts, you can color in the table cells with COLOR tags and specify the font of the dates with the FONT tags.

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 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-2019, Director Online. Article content copyright by respective authors.