reptile7's JavaScript blog is Andrew Peak's personal technical writing project: it focuses on JavaScript and the analysis of JavaScript scripts, although HTML, CSS, and anything else related to coding for the Web are also fair game.

(1) Separate Date objects
(a) for right this moment and
(b) for 12 AM Christmas 1997
are created and respectively assigned to today and xmas variables.

(2) The xmas year is set to the today year. (We wouldn't want to be stuck in 1997, would we?)

(3) The xmas and today getTime( ) returns are subtracted to give the number of milliseconds spanning xmas and today; the milliseconds span is divided by the number of milliseconds in a day to give the number of days spanning xmas and today; the days span is assigned to a daysLeft variable.

(4) daysLeft is rounded up or down to the nearest integer by the Math object's round( ) method.

(5) The integerized daysLeft is concatenated with "only " and " days left until Christmas" strings and the resulting string is appended to some pre-<script> text to give the complete Christmas countdown message.

The small stuff

• The script element does not have a name attribute; a type="text/javascript" attribute is of course what we want.

• We don't need to reach for the getTime( ) method to determine the number of milliseconds spanning xmas and today: a simple xmas - today subtraction will suffice.

• If you want the daysLeft span to always include the current day - I want the daysLeft span to always include the current day - then you should not Math.round( )daysLeft but instead 'ceil' it with a Math.ceil(daysLeft) command.

The problem

It's the xmas.setYear = today.getYear; line that throws a spanner into the works, however; this statement is problematic for two reasons:
(A) It does not actually copy the today year to xmas.
(B) It attempts to make use of the deprecated (read: obsolete) getYear( ) and setYear( ) methods, which are not suited for comparing a pre-2000 Date with a post-2000 Date.

Although it is true that a method is ultimately a property of the object with which it is associated, the today.getYear expression does not evaluate to the today.getYear( ) return and the xmas.setYear = today.getYear; assignment has no effect on the xmas.getYear( ) return; as a result, the original script was really only good for 1997. (This is not to say that the script 'doesn't work' for other years, it just won't give the daysLeft value that you want it to.) For what Jari was trying to do, the correct syntax is:

xmas.setYear(today.getYear( ));

The preceding command would have been OK for 1998 and 1999 but for 2000 and beyond we'll need to use the getFullYear( ) and setFullYear( ) methods instead:

xmas.setFullYear(today.getFullYear( ));

The last six days of the year

Now, what if Christmas has just passed and we're in the 26 December-to-31 December period and we want to know how many days there are before the next Christmas? The above getFullYear( )/setFullYear( ) command places xmas at this year's Christmas; subsequently we can push xmas to next year's Christmas via the following conditional:

if (today > xmas) xmas.setFullYear(today.getFullYear( ) + 1);

The 24th of December

And what if it's 24 December and you would like the script to print out "1 day" as opposed to "1 days"? Why, I think we can arrange that: