Looking for the previous guiStuff?

It's still here, the content didn't go anywhere. You may want to check out this new guiStuff though -- It's rather informative.

References/Tutorials:


Intro Documents:


guiStuff:






::Stuff for the multi-spec coder;

Coding, formats, standards, and other practical things.

 Home  //  JavaScript  //  The Date Object, Part 1 

<!-- JavaScript

Date Object


A Date object is explicitly created in JavaScript unlike a Math object which you don't need to create but it is always present throughout the lifetime of any script. The Date object is one of the most effective and powerful objects. It is a global object and can be used in any part of the code and functions. The Date object can be created by the normal new operator.

var aDate = new Date( );

The Date object can take various types of arguments such as:

var aDate = new Date(yyyy, mm, dd, hh, mm, ss); var aDate = new Date(yyyy, mm, dd);

These are just a couple of formats in which you can use the Date( ) Object, you can change it according to your needs.

Lets have a look on how to display the current date on the browser, well quite simple:

document.write(Date( ));

The above line of code will Display the date in the fashion shown below:

Fri Oct 20 2006 16:28:49 GMT-0500 (Eastern Standard Time);


The time zone and GMT+/- will change depending on the location of the client machine running the browser.

Date object Methods


getDate( )

The getDate() function of the Date object returns the date of the month, i.e., an integer from 1 to 31 signifying the today's date. For example:

var today = new Date("June 17, 2006 00:08:41"); document.write(today.getDate());

This code will output '28' signifying today's date.

getDay( )

The getDay( ) function returns the day of the week. The value returned is a number from 0 to 6. The week starts with a Sunday, so the that is regarded as 0, and Saturday is 6.

var today = new Date("July 28, 2006 00:08:41"); document.write(today.getDay());

The above code will return '5', since that's the number which stands for Friday according to the above explanation.

getHours( )

The getHours( ) function returns a number from 0 to 23 to represent the hour of the day. Even if the current time is 11:59:59 the value returned will be 11 and not 12 even though 12 is the closer value.

var today = new Date("July 28, 2006 00:08:41"); document.write(today.getHours());

So, the output will be 0.

getMinutes( )

The getMinutes( ) function returns the minutes in the time. The value is between 0 and 59.

var today = new Date("July 28, 2006 00:08:41"); document.write(today.getMinutes());

This will return 8 as the output.

getSeconds( )

The getSeconds( ) function returns the second value of the day. The values outputted are from 0 to 59.

var today = new Date("July 28, 2006 00:08:41"); document.write(today.getSeconds());

The result of the above code is 41.

getMilliseconds( )

The getMilliseconds( ) function returns the Millisecond value of the Date object. Therefore the value returned is between 0 and 999.

var today = new Date("July 28, 2006 00:08:41"); document.write(today.getMilliseconds());

Now, since the Milliseconds attribute in the Date object is not specified the output will be 0.

getTime( )

The getTime( ) function will return the value in Milliseconds for the amount of time that has passed since the January 1 1970. This method is widely used in implementation of cookies and their expiration.

var m = new Date(); document.write(m.getTime() + " milliseconds have passed since January 1 1970");

The output at the time of writing this was; 1154032012578.

getTimezoneOffset( )

This method will return the number of minutes of difference between your Time Zone and that of Greenwich Mean Time (GMT). The value can be negative or positive. The method takes care of the Daylight Saving Times. Normally we can make use of this by adding another line of code to convert the minutes into hours so we can obtain the Time Zone of the user.

var m = new Date(); document.write(m.getTimezoneOffset()/60);

The output will be the hours +/- from GMT.

setDate(day)

The setDate method of the Date object is used to set the day to a particular value.

var aDate = new Date(); aDate.setDate(22); document.write(aDate);

The above piece of code will output:

Fri Oct 22 2006 16:28:49 GMT-0500 (Eastern Standard Time)

Thus, the date is changed to 22 and as a consequence so is the day to corresponding to the date value.

setMonth(month, day)

In the setMonth function the month is the compulsory parameter and the day is optional. As the usual syntax goes, the months are defined by 0 to 11 and day is from 1 to 31.

setFullYear(year,month,day)

The setYear( ) function can specify the entire date on the Date object. Only the year parameter is compulsory and the others are optional. The year is to be specified in the 'yyyy' format.

var aDate = new Date(); aDate.setFullYear(1999,4,24); document.write(aDate);

The code above will set the entire date, of course leaving the time aspect the same:

Mon May 24 1999 16:28:49 GMT-0500 (Eastern Standard Time)

All the above get and set functions of the Date objects can be used with UTC time zone. For example, getUTCDate( ) will perform the same function as getDate( ) but it will be with respect to UTC. Similarly there are various such UTC functions like, getUTCDay( ), getUTCMonth( ), getUTCFullYear( ), getUTCHours( ), getUTCMinutes( ), getUTCSeconds( ), setUTCDate( ) and more.

toString( )

The toString( ) function converts the Date object into a string variable.

var aDate = new Date("Sat Jul 22 2006 00:08:41 GMT-0500"); document.write (aDate.toString( ));

toUTCString( )

The toUTCString( ) method converts the time in the Date object to Coordinated Universal Time (UTC).

var aDate = new Date("Sat Jul 22 2006 00:08:41 GMT-0500"); document.write (aDate.toUTCString()); document.write ("<br />" + aDate);

The output of the above code will be as follows:

Fri, 21 Jul 2006 18:38:41 GMT Sat Jul 22 2006 00:08:41 GMT+0530 (India Standard Time)

Date object Properties:
There are no dedicated properties for the Date object but you can use the constructor and prototype properties like in other JavaScript objects.


Return to the JavaScript section, or go the to Main page.





Looking for the old guiStuff?

It's still here, the old content didn't go anywhere.