JS Date Object is used to set the date, time, hour, minute and seconds. It is used to store the current date and time in the program. The date properties of date object are in two forms, local and Greenwich mean time(GMT).
Syntax for Date Object
<br />new Date( );<br />Date Property
Name | Description |
---|---|
constructor | It is used to provide a reference to string functions of the particular object. |
prototype | It is used to add properties and methods |
Example of Date Property
<br /><html><br /><head><br /></head><br /><body><br /><script type="text/javascript"><br /><br /><%%KEEPWHITESPACE%%> var a = new Date();<br /><%%KEEPWHITESPACE%%> document.write("constructor is : " + a.constructor);<br /></script><br /></body><br /></html><br />
- In the above we have used date property.
- We have used constructor property in the program
- var a = new Date(); is used to specify a variable date .
- document.write(“constructor is : ” + a.constructor); is used to provide reference to date string function.
Date Methods
getUTCDay( )Represents a specific date and time according to universal time.
Name | Description | |
---|---|---|
Date( ) | Represents a specific date and time. | |
getDate( ) | Specifies the day of the month. | |
getDay( ) | Specifies the day of the week in range of 0-6. | |
getHours( ) td> | Specifies the number of the hour in range of 0-23. | |
getMinutes( ) | Specifies the number of minute in range of 0-59. | |
getSecond( ) | Specifies the number of second in range of 0-59. | |
getMillisecond( ) | Specifies the number of milliseconds in range of 0-999. | |
getMonth( ) | Specifies the month of the year in range of 0-11. | |
getFullYear( ) | Specifies the year. | |
getTime( ) | Specifies the number of milliseconds since January 1, 1970. | |
getTimezoneOffset( ) | It is used to set the standard time zone about the particular locale. | |
getUTCHour( ) td> | Specifies the number of the hour in range of 0-23 according to universal time. | |
getUTCMinute( ) | Specifies the number of minute in range of 0-59 according to universal time. | |
getUTCsecond( ) | Specifies the number of second in range of 0-59 according to universal time. | |
getUTCMillisecond( ) | Specifies the number of milliseconds in range of 0-999 according to universal time. | |
getUTCMonth( ) | Specifies the month of the year in range of 0-11 according to universal time. | |
getUTCFullYear( ) | Specifies the year according to universal time. | |
getYear( ) | Specifies the year. | |
setDate( ) | It is used to set date and time. | |
setTime( ) | It is used to set the number of milliseconds since January 1, 1970 | |
setHours( ) | It is used to set the number of the hour in range of 0-23. | |
setMinutes( ) | It is used to set the number of minute in range of 0-59. | |
setSeconds( ) | It is used to set the number of second in range of 0-59 | |
setMonth( ) | It is used to set the month of the year in range of 0-11. | |
setMillisecond( ) | It is used to set the number of milliseconds in range of 0-999. | |
setFullYear( ) | It is used to set the complete year. | |
setUTCDate( ) | It is used to set date and time according to universal time. | |
setUTCHours( ) | It is used to set the number of the hour in range of 0-23according to universal time. | |
setUTCMinutes( ) | It is used to set the number of minute in range of 0-59 according to the universal time. | |
setUTCSeconds( ) | It is used to set the number of second in range of 0-59 according to the universal time | |
setUTCMonth( ) | It is used to set the month of the year in range of 0-11according to universal time. | |
setUTCMillisecond( ) | It is used to set the number of milliseconds in range of 0-999 according to universal time. | |
setUTCFullYear( ) | It is used to set the complete year according to universal time. | |
setYear( ) | It is used to set the year. | |
toDateString( ) | It converts date into string value. | |
toTimeString( ) | It converts time into string value. | |
toString( ) | It specifies date object using string. | |
toSource( ) | It is used to return the string using source code of the object. | |
toGMTString( ) | Converts date into string using GMT. | |
toLocaleFormat( ) | Specifies the string of Date format using the locale. | |
toLocaleString( ) | Specifies a string of the Date information using the locale. | |
toLocaleTimeString( ) | It converts time into string value using the locale. | |
toLocaleDateString( ) | It converts date into string value using the locale. | |
toUTCString( ) | It specifies date object using string according to the universal time. | |
valueOf( ) | It is used to return the value of created object of the given parameter . |
Example of Date Methods
<br /><html><br /><br /><body><br /><script type="text/javascript"><br /><%%KEEPWHITESPACE%%> var a = new Date( "March 27, 2014 12:30:00" );<br /><%%KEEPWHITESPACE%%> document.write("Date: " +a);<br /><%%KEEPWHITESPACE%%> document.write("<br>getYear() : " + a.getYear() );<br /><%%KEEPWHITESPACE%%> document.write("<br>getDate() : " + a.getDate() );<br /><%%KEEPWHITESPACE%%> document.write("<br>getHours() : " + a.getHours() );<br /><%%KEEPWHITESPACE%%> document.write("<br>getMinutes() : " + a.getMinutes() );<br /></script><br /></body><br /></html><br />
- In the above example we are using date methods in it.
- We have used few methods in the program i.e. getHours, getMinutes, getYear and getDate.
- In the program we have specified a complete date “var a = new Date( “March 27, 2014 12:30:00″ );” with month, year, date, hour, minutes and seconds
- document.write(“getYear() : ” + a.getYear() ); is used to display only the year.
- document.write(“getDate() : ” + a.getDate() ); it is used to display only the date.
- document.write(“getHours() : ” + a.getHours() ); is used to display the specified hour
- document.write(“getMinutes() : ” + a.getMinutes() ); is used to display the minutes.
JavaScript Date Object Demo
- Save the file as date_methods.html in your system.
- Just open the file in the browser, you will see the below picture in the browser.
When the execution process is completed successfully we will get the following output:
Date Static Methods
Name | Description |
Date.parse( ) | Parse the date and time string which represents milliseconds of the date. |
Date.UTC( ) | Represents milliseconds of the universal date and time. |
Example of Date Static Method
<br /><html><br /><br /><body><br /><script type="text/javascript"><br /><br /><%%KEEPWHITESPACE%%> var a = Date.parse("sept 12, 2001");<br /><%%KEEPWHITESPACE%%> document.write( "Milliseconds from 1970: " + a );<br /><br /><%%KEEPWHITESPACE%%> var b = Date.UTC(2001,9,12);<br /><%%KEEPWHITESPACE%%> document.write( "<br>Milliseconds from 1970: " + b );<br /><br /></script><br /></body><br /></html><br />
- In the above example we are using date static methods.
- We have used two methods in the program i.e. parse and UTC.
- We have specified a date ” var a = Date.parse(“sept 12, 2001″);” is used parse the date in milliseconds from 1970 .
- var b = Date.UTC(2001,9,12); is used to represent milliseconds of universal date and time.
- document.write( “Milliseconds from 1970: ” + a ); is used to display the date in milliseconds.
JavaScript Date Object Demo
- Save the file as static_property.html in your system.
- Just open the file in the browser, you will see the below picture in the browser.