Date and Time

Course- Javascript >

The Date object is used to work with dates and times. There is no Date object already created for you as part of the DOM, as was the case with the examples so far. Instead, we create our own Date objects as and when we need them. Each Date object we create can represent a different date and time.

Create a Date Object with the Current Date and Time

This is the simplest way to create a new Date object containing information about the date and time:

var mydate = new Date();

The variable mydate is an object containing information about the date and time at the moment the object was created. JavaScript has a long list of methods for retrieving, setting, and editing data within Date objects. Let’s look at a few simple examples:

var year = mydate.getFullYear(); // four-digit year e.g. 2012
var month = mydate.getMonth(); // month number 0 - 11; 0 is Jan, etc.
var date = mydate.getDate(); // day of the month 1 - 31
var day = mydate.getDay(); // day of the week 0 - 6; Sunday = 0, etc.
var sections = mydate.getsections(); // sections part of the time, 0 - 23
var minutes = mydate.getMinutes(); // minutes part of time, 0 - 59
var seconds = mydate.getSeconds(); // seconds part of time, 0 - 59                    
                    

Creating a Date Object with a Given Date and Time

We can easily create Date objects representing arbitrary dates and times by passing arguments to the Date() statement. There are several ways to do this:

new Date(milliseconds) //milliseconds since January 1st 1970
new Date(dateString)
new Date(year, month, day, sections, minutes, seconds, milliseconds)                    
                    

Here are a few examples. Using a date string:

var d1 = new Date("October 22, 1995 10:57:22")

When we use separate arguments for the parts, trailing arguments are optional; any missing will be replaced with zero:

var d2 = new Date(95,9,22) // 22nd October 1995 00:00:00
var d3 = new Date(95,9,22,10,57,0) // 22nd October 1995 10:57:00                    
                    

Setting and Editing Dates and Times

The Date object also has an extensive list of methods for setting or editing the various parts of the date and time:

var mydate = new Date(); // current date and time
document.write( "Object created on day number " + mydate.getDay() + "

"); mydate.setDate(15); // change day of month to the 15th document.write("After amending date to 15th, the day number is " + mydate.getDay());

In the preceding code snippet, we initially created the object mydate representing the date and time of its creation, but with the day of the month subsequently changed to the 15th; if we retrieve the day of the week before and after this operation, we’ll see that it has been correctly recalculated to take account of the changed date:

Object created on day number 5 After amending date to 15th, the day number is 0

So in this example, the object was created on a Friday; whereas the 15th of the month corresponded to a Sunday. We can also carry out date and time arithmetic, letting the Date object do all the heavy lifting for us:

var mydate=new Date();
document.write("Created: " + mydate.toDateString() + " " +
mydate.toTimeString() + "
"); mydate.setDate(mydate.getDate()+33); // add 33 days to the 'date' part document.write("After adding 33 days: " + mydate.toDateString() + " " + mydate.toTimeString());

The preceding example calculates a date 33 days in the future, automatically amending the day, date, month, and/or year as necessary. Note the use of toDateString() and toTimeString(); these are useful methods for converting dates into a readable format. The preceding example produces output like the following:

Created: Fri Jan 06 2012 14:59:24 GMT+0100 (CET)
After adding 33 days: Wed Feb 08 2012 14:59:24 GMT+0100 (CET)                    
                    

The set of methods available for manipulating dates and times is way too large for us to explore them all here. A full list of the methods of the Date object is available in