The Javascript date() method of date object returns today's date and time. Date object are usually created using new Date() constructor. Use it for example to get today's date and time.
In Javascript to work with date and time, create an instance of date() object with new keyword.
There are different way to create a date object in Javascript.
Here is how you can create a Date object for each of the ways above:
Syntax
<html>
<head>
<title>Javascript Date Object</title>
var my_date=new Date("August 15, 1947 13:14:00");
var my_date=new Date("August 15, 1947");
var my_date=new Date(88,09,12,13,14,00);
var my_date=new Date(88,09,12);
var my_date=new Date(500);
</head>
<body>
</body>
</html>
Note: Paramters in the brackets are always optional
Once created an instance of the Date object, you can access all the methods of the date object from intance variable.
Here is a list of each date property with description.
Property | Description |
---|---|
constructor | Specifies the function that creates an object's prototype. |
prototype | This will allows you to add properties and function to an object. |
Here is a list of each function of date object with description.
Functions | Description |
---|---|
Date() | This will returns today's date and time |
getDate() | This will returns the day of the month for the specified date. |
getDay() | This will returns the day of the week for the specified date. |
getFullYear() | This will returns the year of the specified date. |
getHours() | This will returns the hour in the specified date. |
getMilliseconds() | This will returns the milliseconds in the specified date. |
getMinutes() | This will returns the minutes in the specified date. |
getMonth() | This will returns the month in the specified date. |
getSeconds() | This will returns the seconds in the specified date. |
getTime() | This will returns the numeric value of the specified date. |
getTimezoneOffset() | This will returns the time-zone offset in minutes for the current locale. |
getUTCDate() | This will returns the day of the month in the specified date according to universal time. |
getUTCDay() | This will returns the day of the week in the specified date according to universal time. |
getUTCFullYear() | This will returns the year in the specified date according to universal time. |
getUTCHours() | This will returns the hours in the specified date according to universal time. |
getUTCMilliseconds() | This will returns the milliseconds in the specified date according to universal time. |
getUTCMinutes() | This will returns the minutes in the specified date according to universal time. |
getUTCMonth() | This will returns the month in the specified date according to universal time. |
getUTCSeconds() | This will returns the seconds in the specified date according to universal time. |
getYear() | This will returns the year in the specified date according to local time. |
setDate() | This will sets the day of the month for a specified date. |
setFullYear() | This will sets the full year for a specified date. |
setHours() | This will sets the hours for a specified date. |
setMilliseconds() | This will sets the milliseconds for a specified date. |
setMinutes() | This will sets the minutes for a specified date. |
setMonth() | This will sets the month for a specified date. |
setSeconds() | This will sets the seconds for a specified date. |
setTime() | This will sets the Date object to the time represented by a number of milliseconds. |
setUTCDate() | This will sets the day of the month for a specified date according to universal time. |
setUTCFullYear() | This will sets the full year for a specified date according to universal time. |
setUTCHours() | This will sets the hour for a specified date according to universal time. |
setUTCMilliseconds() | This will sets the milliseconds for a specified date according to universal time. |
setUTCMinutes() | This will sets the minutes for a specified date according to universal time. |
setUTCMonth() | This will sets the month for a specified date according to universal time. |
setUTCSeconds() | This will sets the seconds for a specified date according to universal time. |
setYear() | This will sets the year for a specified date according to local time. |
toDateString() | This will returns the "date" portion of the Date as a human-readable string. |
toGMTString() | This will converts a date to a string, using the Internet GMT conventions. |
toLocaleDateString() | This will returns the "date" portion of the Date as a string, using the current locale's conventions. |
toLocaleString() | This will converts a date to a string, using the current locale's conventions. |
toLocaleTimeString() | This will returns the "time" portion of the Date as a string, using the current locale's conventions. |
toString() | This will returns a string representing the specified Date object. |
toTimeString() | Returns the "time" portion of the Date as a human-readable string. |
toUTCString() | This will converts a date to a string, using the universal time convention. |
valueOf() | This will returns the primitive value of a Date object. |
<HTML>
<TITLE>Example of Number Object</TITLE>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
//Get Date example
var d = new Date();
document.write(d.getDate());
document.write(".");
document.write(d.getMonth() + 1);
document.write(".");
document.write(d.getFullYear());
document.write("</br>")
//UTC Time example
document.write(d.getUTCHours())
document.write(".")
document.write(d.getUTCMinutes() + 1)
document.write(".")
document.write(d.getUTCSeconds())
document.write("</br>")
</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>
Above example will produce following result
//outputs of get date
25.11.2014
//outputs of UTC time
3.47.4