In JavaScript Date class is used to store and retrieve dates information. To create a
JavaScript Date object
we can use any of the following techniques.
Above line will create a new Date object with the value of the current date and
time on that PC where the browser is running..
(We'll use this technique later on to display the current date on the page)
First of all we need to store the date in a variable data:
Following are the methods to extract information from the
Date object: in JavaScript
| Method of Date object |
Description of JavaScript Date Object |
Example of JavaScript Date |
getFullYear () |
Retrieves the year component of the date as a
4-digit number. |
var x = new Date();
y = x.getFullYear (); |
getMonth() |
getMonth() Function of Date Object retrieves the month component of the date as a
number from 0 to 11 (0=January, 1=February, etc) |
var x = new Date();
y = x.getMonth() |
getDate() |
getDate() Function of Date Object retrieves the day-of-month component of the date as
a number from 1 to 31 |
var x = new Date();
y = x.getDate() |
getDay() |
getDay() Function of Date Object retrieves the day of the week component of the date
as a number from 0 to 6 (0=Sunday, 1=Monday, etc) |
var x = new Date();
y = x.getDay() |
getHours() |
getHours() Function of Date Object retrieves the hours component of the date as a
number from 0 to 23 |
var x = new Date();
y = x.getHours() |
getMinutes() |
getMinutes() Function of Date Object retrieves the minutes component of the date as a
number from 0 to 59 |
var x = new Date();
y = x.getMinutes() |
getSeconds() |
getSeconds() Function of Date Object retrieves the seconds component of the date as a
number from 0 to 59 |
var x = new Date();
y = x.getSeconds() |
getMilliseconds() |
getMilliseconds() Function of Date Object retrieves the milliseconds component of the date as
a number from 0 to 999 |
var x = new Date();
y = x.getMilliseconds() |
getTimezoneOffset() |
getTimezoneOffset() Function of Date Object retrieves the time difference, in minutes, between
the computer's local time and GMT |
var x = new Date();
y = x.getTimezoneOffset() |
The above JavaScript Date methods are similar to the previous section of
JavaScript Tutorial [
JavaScript
with Objects], but they work
with
UTC (Universal Time Coordinated) time, or GMT..