// Create two date objects, today and Xmas, to hold today's date and Xmas date

var today = new Date();
var Xmas = new Date();
var diff, daysToXmas, weeksToXmas;

This section of code creates two date objects, today and Xmas. Recall that an object is a scheme to group together like properties and methods. The date object contains properties that are pertinent to a specific date, such as the day, month, hour, second, etc. The date object also contains methods that are related to dates, such as changing the hour, or calculating the number of milliseconds that have passed since January 1, 1970. The date object is just one of many of JavaScript's inbuilt objects. It will be discussed in greater detail in later articles.

The var keyword reserves a variable called today, and the new keyword is used to create a date object. The object is then stored in the variable today (or Xmas in the second assignment). When a date object is instantiated (created with specific values), it can be created with a specific day. For example, the following code would create a date object holding the millinium's date:

var millinium = new Date(1, 1, 2000);

If no parameters are sent to the date constructor (as in Xmas.htm), the date object is created with the current day and time. Therefore, when we assign new values to the day and month properties of Xmas, it is not necessary to change the year, as it is created with the current year.

Finally, three 'normal' variables are created - diff, daysToXmas and weeksToXmas. diff will be used to temporarily store the difference between Christmas and the current day. diff will then be converted into days and weeks, and stored in the other two variables.

Close window