Thursday, August 9, 2007

Date() Object In JavaScript

Dates and times creates a series of special problems to the computer programming particularly Web programmer who has to supply web Content to a global audience. For Example:Some countries present the date as dd/mm/yy while others use mm/dd/yy.

In JavaScript Date Object is a part of the overall specification of JavaScript and is available to any page that can include JavaScript,you create a new date object instance by creating a variable as follows: dtObj = new Date( );

The Date( ) object doesn't have properties that can be directly read and written. Instead it has methods that are used to work with the data value in the object.



Program:

<html>
<head>
<title>Date Object In JavaScript</title>
</head>
<body>
<script language="javascript">
dtObj = new Date();

document.write
("Return the day of the month "+"("+dtObj.getDate( )+")"+".<br/><br/>");

// O/P Will Be == > Return the day of the month 9

document.write
("Return the day of the week "+"("+dtObj.getDay( )+")"+ ".<br/><br/>");

// O/P Will Be == > Return the day of the week 4.

document.write
("Return the four digit year "+"("+dtObj.getFullYear( )+")"+ ".<br/><br/>");

// O/P Will Be == > Return the four digit year 2007.

document.write
("Return the hours "+"("+dtObj.getHours( )+")"+ ".<br/><br/>");

// O/P Will Be == > Return the hours 18.

document.write
("Return the milliseconds "+"("+dtObj.getMilliseconds( )+")"+ ".<br/><br/>");

// O/P Will Be == > Return the milliseconds 125.

document.write
("Return the minutes "+"("+dtObj.getMinutes( )+")"+ ".<br/><br/>");

// O/P Will Be == > Return the minutes 29.

document.write
("Return the month "+"("+dtObj.getMonth( )+")" + ".<br/><br/>");

// O/P Will Be == > Return the month 7.

document.write
("Return the seconds "+"("+dtObj.getSeconds( )+")" + ".<br/><br/>");

// O/P Will Be == > Return the seconds 17

document.write
("Return the internal representation of the time "+"("+dtObj.getTime( )
+")"+ ".<br/><br/>");

// O/P Will Be == > Return the internal representation of the time 1186664357125.

document.write
("Return the offset from GMT "+"("+dtObj.getTimezoneOffset( ) +")"+ ".<br/><br/>");

// O/P Will Be == > Return the offset from GMT -330.

document.write
("Return the Universal Time day of the month "+"("+dtObj.getUTCDate( )+")" + ".<br/><br/>");

// O/P Will Be == > Return the Universal Time day of the month 9.

document.write
("Return the Universal Time day of the week "+"("+dtObj.getUTCDay( )+")"+ ".<br/><br/>");

// O/P Will Be == > Return the Universal Time day of the week 4.

document.write
("Return the Universal Time four digit year "+"("+dtObj.getUTCFullYear( )+")" + ".<br/><br/>");

// O/P Will Be == > Return the Universal Time four digit year 2007.

document.write
("Return the Universal Time hours "+"("+dtObj.getUTCHours( )+")" + ".<br/><br/>");

// O/P Will Be == > Return the Universal Time hours 12.

document.write
("Return the Universal Time milliseconds "+"("+dtObj.getUTCMilliseconds( )+")" + ".<br/><br/>");

// O/P Will Be == > Return the Universal Time milliseconds 125.

document.write
("Return the Universal Time minutes "+"("+dtObj.getUTCMinutes( ) +")"+ ".<br/><br/>");

// O/P Will Be == > Return the Universal Time minutes 59.

document.write
("Return the Universal Time month "+"("+dtObj.getUTCMonth( )+")"+ ".<br/><br/>");

// O/P Will Be == > Return the Universal Time month 7.

document.write
("Return the Universal Time seconds "+"("+dtObj.getUTCSeconds( )+")"+".<br/><br/>");

// O/P Will Be == > Return the Universal Time seconds 17.

document.write
("The time and date at Your computer's location "+"("+dtObj.toLocaleString()+")" + ".<br/><br/>");

//The time and date at Your computer's location Thursday, August 09, 2007 6:29:17 PM.

document.write
("The time zone offset B/W local time and GMT is "+"("+dtObj.getTimezoneOffset()+")" + " minutes.<br/><br/>");

//The time zone offset B/W local time and GMT is -330 minutes.

document.write
("The time and date (GMT) is: "+"("+dtObj.toGMTString()+")" + ".<br/><br/>");

//The time and date (GMT) is: Thu, 9 Aug 2007 12:59:17 UTC.

</script>
</body>
</html>

Note: Output In Coment Line are which i got as result when i Compiled in My System,
so date and time will change according your system time and day.

No comments: