Date in JavaScript
-
The Date object is used to work with date and time.It is used for 2 tasks:
-
Storing dates and time
-
Retrieve current date and time
Date Constructors
-
Date( ): This creates a Date object with the current date and time of browser’s PC.
var now=new Date( );
-
Date(“Month dd,yyyy”)
var now;
now=new Date(“February 4,2013”);
-
Date(yy,mm,dd)
var now;
now=new Date(13,2,4);
-
Date(“Month dd,yyyy hh:mm:ss” ):
var now;
now=new Date(“February 4,2013 17:45:00”);
-
Date(yy,mm,dd,hh,mm,ss)
var now;
now=new Date(13,2,4,17,45,0);
-
Date(milliseconds) Creates a date object with the date value represented by number of milliseconds passed since midnight Jan 1,1970
var now;
now=new Date(500);
Using Date Methods
-
getDate( ) Returns day of the month.
-
getDay( ) Returns day of the week from 0 to 6.
-
getFullYear( ) Returns year with 4 digits.
-
getMonth( ) Returns month from 0 to 11.
-
getHours( ) Returns the hour (0 to 23).
-
getMinutes( ) Returns the minute component.
-
getSeconds( ) Returns the seconds component.
- toString( ) Returns the string representation of Date object
var d=new Date();
var str=d.toString();The result of str will be:
Mon Feb 04 2013 12:30:59 GMT+0530 (India Standard Time)
-
toDateString( ) Returns the string representation of date portion of Date object
var d=new Date();
var str=d.toDateString();The result of str will be:
Mon Feb 04 2013
-
toTimeString( ) Returns the string representation of time portion of Date object
var d=new Date();
var str=d.toTimeString();The result of str will be:
12:33:39 GMT+0530 (India Standard Time)