The Date object is used to work
with dates and times. You create an instance of the Date object with the
"new" keyword.The JavaScript
date object can be used to get year, month and day. You can display a
timer on the webpage by the help of JavaScript date object. Once a Date object
is created, a number of methods allow you to operate on it. Most methods simply
allow you to get and set the year, month, day, hour, minute, second, and
millisecond fields of the object, using either local time or UTC (universal, or
GMT) time.
Example
<!DOCTYPE html>
<html>
<body>
<p> getFullYear()
method returns the 'full year' of a date:</p>
<p id="output"></p>
<script>
var d = new Date();
document.getElementById("output").innerHTML =
d.getFullYear();
</script>
</body>
</html>
|
| OUTPUT:- getFullYear() method returns the 'full year' of a date:
2017
|
You can use different Date constructors to create date object. It provides methods to get and set day, month, year, hour, minute and seconds.
Date Object
Methods
Method
|
Description
|
getDate()
|
Returns
the day of the month (from 1-31)
|
Returns
the day of the week (from 0-6)
|
|
getDay()
|
Returns
the year
|
Returns
the hour (from 0-23)
|
|
getFullYear()
|
Returns
the milliseconds (from 0-999)
|
Returns
the minutes (from 0-59)
|
|
getHours()
|
Returns
the month (from 0-11)
|
Returns
the seconds (from 0-59)
|
|
getMilliseconds()
|
Returns
the number of milliseconds since midnight Jan 1 1970, and a specified date
|
Returns
the time difference between UTC time and local time, in minutes
|
|
getMinutes()
|
Returns
the day of the month, according to universal time (from 1-31)
|
Returns
the day of the week, according to universal time (from 0-6)
|
|
getMonth()
|
Returns
the year, according to universal time
|
Returns
the hour, according to universal time (from 0-23)
|
|
getSeconds()
|
Returns
the milliseconds, according to universal time (from 0-999)
|
Returns
the minutes, according to universal time (from 0-59)
|
|
getTime()
|
Returns
the month, according to universal time (from 0-11)
|
Returns
the seconds, according to universal time (from 0-59)
|
|
getTimezoneOffset()
|
Deprecated.
Use the getFullYear() method instead
|
getUTCDate()
|
Returns
the number of milliseconds since midnight Jan 1, 1970
|
Parses a
date string and returns the number of milliseconds since January 1, 1970
|
|
getUTCDay()
|
Sets the
day of the month of a date object
|
Sets the year
of a date object
|
|
getUTCFullYear()
|
Sets the
hour of a date object
|
Sets the
milliseconds of a date object
|
|
getUTCHours()
|
Set the
minutes of a date object
|
Sets the
month of a date object
|
|
getUTCMilliseconds()
|
Sets the
seconds of a date object
|
Sets a
date to a specified number of milliseconds after/before January 1, 1970
|
|
getUTCMinutes()
|
Sets the
day of the month of a date object, according to universal time
|
Sets the
year of a date object, according to universal time
|
|
getUTCMonth()
|
Sets the
hour of a date object, according to universal time
|
Sets the
milliseconds of a date object, according to universal time
|
|
getUTCSeconds()
|
Set the
minutes of a date object, according to universal time
|
Sets the
month of a date object, according to universal time
|
|
getYear()
|
Set the
seconds of a date object, according to universal time
|
now()
|
Deprecated.
Use the setFullYear() method instead
|
parse()
|
Converts
the date portion of a Date object into a readable string
|
Deprecated.
Use the toUTCString() method instead
|
|
setDate()
|
|
Returns
the date as a string, using the ISO standard
|
|
setFullYear()
|
Returns
the date as a string, formatted as a JSON date
|
Returns
the date portion of a Date object as a string, using locale conventions
|
|
setHours()
|
Returns
the time portion of a Date object as a string, using locale conventions
|
Converts a
Date object to a string, using locale conventions
|
|
setMilliseconds()
|
Converts a
Date object to a string
|
Converts
the time portion of a Date object to a string
|
Date Example-1
Current Date and Time: <span id="txt"></span>
<script>
var today=new Date();
document.getElementById('txt').innerHTML=today;
</script>
|
Output:
Current Date
and Time: Fri Jun 16 2017 11:12:02 GMT+0530 (India Standard Time)
|
Date Example-2
<!DOCTYPE html>
<html>
<body>
<p>The getTime()
function returns the number of milliseconds since then:</p>
<p id="output"></p>
<script>
var d = new Date();
document.getElementById("output").innerHTML =
d.getTime();
</script>
</body>
</html>
|
| Output:-
The getTime() function returns the number of milliseconds since then:
1499943543848
|
Date Example-3
print current time of system.
Current Time: <span id="txt"></span>
<script>
var today=new Date();
var
h=today.getHours();
var
m=today.getMinutes();
var
s=today.getSeconds();
document.getElementById('txt').innerHTML=h+":"+m+":"+s;
</script>
|
Output:
Current Time:
11:12:2
|