JavaScript Date Object
In this JavaScript tutorial, you will learn about date object and methods of date object explained along with syntax and example.
JavaScript Date Object:
Usage of 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.
For example:
var exf=new Date( )
In the above example, variable exf is defined as Date object which has current date and time as its initial value.
Methods of Date Object:
Some of the methods available with Date object are:
setSeconds()– Sets the seconds for a specified date according to local time.
setMinutes() – Sets the minutes for a specified date according to local time.
setHours() – Sets the hours for a specified date according to local time.
setDate() – Sets the day of the month for a specified date according to local time.
setMonth() – Sets the month for a specified date according to local time.
setYear() – Sets the year (deprecated) for a specified date according to local time.
setFullYear() – Sets the full year for a specified date according to local time.
toString() – Returns a string representing the specified Date object.
getSeconds() – Returns seconds in the specified date according to local time.
getMinutes() – Returns minutes in the specified date according to local time.
getHours() – Returns hour in the specified date according to local time.
getDay() – Returns day of the week for a specified date according to local time
getDate() – Returns day of the month for a specified date according to local time.
getMonth() – Returns month in the specified date according to local time.
getYear() – Returns year (deprecated) in the specified date according to local time.
getFullYear() – Returns year of the specified date according to local time.
Example for usage of Date Object methods mentioned above:
var exf=new Date()
exf.setFullYear(2020,0,20)
As we have seen setFullYear() is used for Setting the full year for a specified date according to local time. In the above example the Date object exf is set to the specific date and year 20th January 2020
Example for using methods of Date Object
|
Output of the above program is:
11/15/2006
In the above example:
- A variable exforsys is declared.
. - Then a new instance of the date() object is created using new keyword and is assigned to exforsys variable.
. - After this statement a variable called currentDay is declared and this is given the value of current day of the month. This is done by using the value of the variable exfrosys, along with dot operator in conjunction with getDate() method.
. - After this statement a variable called currentMonth is declared and this is given the value of current month. This is done by using the value of variable exfrosys, along with dot operator in conjunction with getMonth() method. Here one can see that we have added "1" to the value of method before assigning it to month variable. This is because months are counted, starting with "0", i.e. January = "0", December ="11". By adding "1", it now becomes the current month. The same concept must be used while getting day of the week also.
. - After this statement a variable called currentYear is declared and this is given the value of current Year. This is done by using the value of variable exfrosys, along with dot operator in conjunction with getFullYear() method.
. - At last, a string is created to print out the current full date, using a document.write statement.