Javascript Dates and Time

Javascript has a lot of inbuilt methods you can use to get the date and time. Create a new web page from your template and try out the code for the dates and times used in this section. (However, all the value come from your computer. If the your clock has the wrong time or date then Javascript won't correct it.)

The first thing you need to do is to create a new Date object:

var the_date = new Date();

Note the use of the keyword new. After a space you type the word Date (capital "D"). This is followed by a pair of round brackets. This is enough to create a new date object, which we've assign to a variable called the_date. To see what this prints out, add the following line:

document.write( the_date + "<BR>");

You should find that it prints out quite a lot of information: the day of the week, the month, the day of the month, and then the time. You'll also find that the Date object prints out information to do with GMT (or UTC as it's now known). It will tell you how far ahead or behind the Greenwich Meridian you are.

You can, however, add arguments between the round brackets of Date. The full list of arguments is:

Date( year, month, day [, hour, minute, second, millisecond ] )

Only the first three arguments are required. Try your birthday between the round brackets of Date:

var birthday = (1990, 10, 25);

For the month value, January is 0 and December is 11. Hour values are from 0 to 23.

 

In the next part, you'll see how to get the various parts of a date, and how to customise them.