|
|
|
JavaScript : Date Object
Date object of Java Script is used to work with date and times.
General syntax for defining Date object in Java Script is as follows:
var variablename=new Date( )
In the above new is a keyword which creates an instance of object and
Date() defines variablename as Date Object.
Example:
var d=new Date( )
In the above example, variable d is defined as Date object which has current
date and time as its initial value.
List of Date method and it's description is given below.
|
| Methods |
Description |
| getFullYear() |
Returns year in full 4 digit format (ie:
2004). |
| getYear() |
Returns the year, in 4 digit format or
otherwise depending on browser. Deprecated in favor of
getFullYear(). |
| getMonth() |
Returns the month. (Range is 0-11)! |
| getDate() |
Returns the day of the month (Range is
1-31) |
| getDay() |
Returns the day of the week (Range is 0-6).
0=Sunday, 1=Monday, etc. |
| getHours() |
Returns the hour (Range is 0-23). |
| getMinutes() |
Returns the minutes. (Range is 0-59). |
| getSeconds() |
Returns the seconds. (Range is 0-59). |
| getMilliseconds() |
Returns the milliseconds. (Range is
0-999). |
| getTime() |
Returns the millisecond representation of the
current Date object. In the words, the number of milliseconds between
1/1/1970 (GMT) and the current Date object. |
| getTimezoneOffset() |
Returns the offset (time difference) between
Greenwich Mean Time (GMT) and local time of Date object, in minutes. |
| getUTCFullYear() |
Returns the full 4 digit year in Universal
time. |
| getUTCMonth() |
Returns the month in Universal time. |
| getUTCDate() |
Returns the day of the month in Universal
time. |
| getUTCDay() |
Returns the day of the week in Universal
time. |
| getUTCHours() |
Returns the hour in Universal time. |
| getUTCMinutes() |
Returns the minutes in Universal time. |
| getUTCSeconds() |
Returns the seconds in Universal time. |
| getUTCMilliseconds() |
Returns the milliseconds in Universal
time. |
|
<html>
<head>
<title>My Web Page</title>
</head>
<body>
<script type="text/javascript">
var d = new Date();
document.write(d.getUTCDate());
</script>
</body>
</html>
|
| Methods |
Description |
| setFullYear(year, [month], [day]) |
Sets the year of the Date object. (year: 4
digit year). |
| setYear(year) |
Sets the year of the Date object. "year" can be
two digits (1900 is automatically added to it), or 4 digits. Deprecated
over setFullYear() above. |
| setMonth(month, [day]) |
Sets the month of the Date object. (month:
0-11) |
| setDate(day_of_month) |
Sets the day of the month of the Date object.
(day_of_month: 1-31). |
| setHours(hours, [minutes], [seconds],
[millisec]) |
Sets the hour of the Date object. (hours:
0-23). |
| setMinutes(minutes, [seconds], [millisec]) |
Sets the minutes of the Date object. (minutes:
0-59). |
| setSeconds(seconds, [millisec]) |
Sets the seconds of the Date object. (seconds:
0-59). |
| setMilliseconds(milli) |
Sets the milliseconds field of the Date object.
(milli: 0-999) |
| setTime(milli) |
Sets the value of the Date object in terms of
milliseconds elapsed since 1/1/1970 GMT. |
| setUTCFullYear(year, [month], [day]) |
Sets the year of the Date object in Universal
time. |
| setUTCMonth(month, [day]) |
Sets the month in Universal time. |
| setUTCDate(day_of_month) |
Sets the day of the month in Universal
time. |
| setUTCHours(hours, [minutes], [seconds],
[millisec]) |
Sets the hours in Universal time. |
| setUTCMinutes(minutes, [seconds],
[millisec]) |
Sets the minutes in Universal time. |
| setUTCSeconds(seconds, [millisec]) |
Sets the seconds in Universal time. |
| setUTCMilliseconds(milli) |
Sets the milliseconds in Universal time. |
|
<html>
<head>
<title>My Web Page</title>
</head>
<body>
<script type="text/javascript">
var d = new Date();
d.setFullYear(2009,05,25);
document.write(d);
</script>
</body>
</html>
|
| Methods |
Description |
| toGMTString() |
Converts a date to a
string, using the GMT conventions. Deprecated in favor of
toUTCString(). |
| toLocaleString() |
Converts a date to a
string, using current locale conventions. |
| toLocaleDateString() |
Returns the date portion
of the Date as a string, using current locale conventions. |
| toLocaleTimeString() |
Returns the time portion
of the Date as a string, using current locale conventions. |
| toString() |
Converts a Date to human-readable string. |
| toUTCString() |
Converts a Date to human-readable string, in
Universal time. |
| parse(datestring) |
Returns the number of
milliseconds in a date string since 1/1/1970. (datestring: a string
containing the date/time to be parsed). |
| UTC(year, month, [day], [hours], [minutes],
[seconds], [milli]) |
Returns the number of
milliseconds in a date string since 1/1/1970, Universal time. |
| valueOf() |
Converts a Date to milliseconds. Same as
getTime();. |
|
Note: "[]" surrounding a parameter below
means the parameter is optional.
<html>
<head>
<title>My Web Page</title>
</head>
<body>
<script type="text/javascript">
var d = new Date();
document.write(d.toLocaleDateString());
</script>
</body>
</html>
|
Previous Next
Contact Us
|
|
|