HOME

JS Basic
JS HOME
JS Introduction
JS How To
JS Where To
JS Variables
JS Operators
JS Functions
JS Conditional
JS Looping
JS Guidelines

JS References
JS String Object
JS Array Object
JS Date Object
JS Math Object
JS Window Object
JS Frame Object
JS Form Object
JS Browser Object

Examples/Quiz
JS Examples
JS Quiz Test
Download XMLSpy here
Please visit our sponsors !

JavaScript Date Object

previous next

The Date object is used to work with dates and times.


Examples

Date
Returns today's date including date, month, and year. Note that the getMonth method returns 0 in January, 1 in February etc. So add 1 to the getMonth method to display the correct date.

Time
Returns the current local time including hour, minutes, and seconds. To return the GMT time use getUTCHours, getUTCMinutes etc.

Set date
You can also set the date or time into the date object, with the setDate, setHour etc. Note that in this example, only the FullYear is set.

UTC time
The getUTCDate method returns the Universal Coordinated Time which is the time set by the World Time Standard.

Display weekday
A simple script that allows you to write the name of the current day instead of the number. Note that the array object is used to store the names, and that Sunday=0, Monday=1 etc.

Display full date
How to write a complete date with the name of the day and the name of the month.

Display time
How to display the time on your pages. Note that this script is similar to the Time example above, only this script writes the time in an input field. And it continues writing the time one time per second.


The Date object

The Date object is used to work with dates and times. 

You create an instance of the Date object with the "new" keyword.

To store the current date in a variable called "my_date":

var my_date=new Date()

After creating an instance of the Date object, you can access all the methods of the object from the "my_date" variable. If, for example, you want to return the date (from 1-31) of a Date object, you should write the following:

my_date.getDate()

You can also write a date inside the parentheses of the Date() object, like this:

new Date("Month dd, yyyy hh:mm:ss")
new Date("Month dd, yyyy")
new Date(yy,mm,dd,hh,mm,ss)
new Date(yy,mm,dd)
new Date(milliseconds)

Here is how you can create a Date object for each of the ways above:

var my_date=new Date("October 12, 1988 13:14:00")
var my_date=new Date("October 12, 1988")
var my_date=new Date(88,09,12,13,14,00)
var my_date=new Date(88,09,12)
var my_date=new Date(500)

The Date object's methods are described below:

NN: Netscape, IE: Internet Explorer, ECMA: Web Standard

Methods Explanation NN IE ECMA
Date() Returns a Date object 2.0 3.0 1.0
getDate() Returns the date of a Date object (from 1-31) 2.0 3.0 1.0
getDay() Returns the day of a Date object (from 0-6. 0=Sunday, 1=Monday, etc.) 2.0 3.0 1.0
getMonth() Returns the month of a Date object (from 0-11. 0=January, 1=February, etc.) 2.0 3.0 1.0
getFullYear() Returns the year of a Date object (four digits) 4.0 4.0 1.0
getYear() Returns the year of a Date object (from 0-99). Use getFullYear instead !! 2.0 3.0 1.0
getHours() Returns the hour of a Date object (from 0-23) 2.0 3.0 1.0
getMinutes() Returns the minute of a Date object (from 0-59) 2.0 3.0 1.0
getSeconds() Returns the second of a Date object (from 0-59) 2.0 3.0 1.0
getMilliseconds() Returns the millisecond of a Date object (from 0-999) 4.0 4.0 1.0
getTime() Returns the number of milliseconds since midnight 1/1-1970 2.0 3.0 1.0
getTimezoneOffset() Returns the time difference between the user's computer and GMT 2.0 3.0 1.0
getUTCDate() Returns the date of a Date object in universal (UTC) time 4.0 4.0 1.0
getUTCDay() Returns the day of a Date object in universal time 4.0 4.0 1.0
getUTCMonth() Returns the month of a Date object in universal time 4.0 4.0 1.0
getUTCFullYear() Returns the four-digit year of a Date object in universal time 4.0 4.0 1.0
getUTCHours() Returns the hour of a Date object in universal time 4.0 4.0 1.0
getUTCMinutes() Returns the minutes of a Date object in universal time 4.0 4.0 1.0
getUTCSeconds() Returns the seconds of a Date object in universal time 4.0 4.0 1.0
getUTCMilliseconds() Returns the milliseconds of a Date object in universal time 4.0 4.0 1.0
parse() Returns a string date value that holds the number of milliseconds since January 01 1970 00:00:00 2.0 3.0 1.0
setDate() Sets the date of the month in the Date object (from 1-31) 2.0 3.0 1.0
setFullYear() Sets the year in the Date object (four digits) 4.0 4.0 1.0
setHours() Sets the hour in the Date object (from 0-23) 2.0 3.0 1.0
setMilliseconds() Sets the millisecond in the Date object (from 0-999) 4.0 4.0 1.0
setMinutes() Set the minute in the Date object (from 0-59) 2.0 3.0 1.0
setMonth() Sets the month in the Date object (from 0-11. 0=January, 1=February) 2.0 3.0 1.0
setSeconds() Sets the second in the Date object (from 0-59) 2.0 3.0 1.0
setTime() Sets the milliseconds after 1/1-1970 2.0 3.0 1.0
setYear() Sets the year in the Date object (00-99) 2.0 3.0 1.0
setUTCDate() Sets the date in the Date object, in universal time (from 1-31) 4.0 4.0 1.0
setUTCDay() Sets the day in the Date object, in universal time (from 0-6. Sunday=0, Monday=1, etc.) 4.0 4.0 1.0
setUTCMonth() Sets the month in the Date object, in universal time (from 0-11. 0=January, 1=February) 4.0 4.0 1.0
setUTCFullYear() Sets the year in the Date object, in universal time (four digits) 4.0 4.0 1.0
setUTCHour() Sets the hour in the Date object, in universal time (from 0-23) 4.0 4.0 1.0
setUTCMinutes() Sets the minutes in the Date object, in universal time (from 0-59) 4.0 4.0 1.0
setUTCSeconds() Sets the seconds in the Date object, in universal time (from 0-59) 4.0 4.0 1.0
setUTCMilliseconds() Sets the milliseconds in the Date object, in universal time (from 0-999) 4.0 4.0 1.0
toGMTString() Converts the Date object to a string, set to GMT time zone 2.0 3.0 1.0
toLocaleString() Converts the Date object to a string, set to the current time zone 2.0 3.0 1.0
toString() Converts the Date object to a string 2.0 4.0 1.0


previous next

Jump to: Top of Page or HOME or Printer Friendly Printer friendly page


Search W3Schools:


What Others Say About Us

Does the world know about us? Check out these places:

Dogpile Alta Vista MSN Google Excite Lycos Yahoo Ask Jeeves


We Help You For Free. You Can Help Us!


W3Schools is for training only. We do not warrant its correctness or its fitness for use. The risk of using it remains entirely with the user. While using this site, you agree to have read and accepted our terms of use and privacy policy.

Copyright 1999-2002 by Refsnes Data. All Rights Reserved


Validate How we converted to XHTML Validate

ASPHits
Active Server Pages Resource Web Site


Your own Web Site?

Read W3Schools
Hosting Tutorial



$15 Domain Name
Registration
Save $20 / year!



Advertise
at W3Schools

Only 0.5 CPM



SELECTED LINKS

University Online
Master Degree
Bachelor Degree


Web Software

The Future of
Web Development


Jobs and Careers

Web Security
Web Statistics
Web Standards


PARTNERS

W3Schools
TopXML
VisualBuilder
XMLPitstop
DevelopersDex
DevGuru
Programmers Heaven
The Code Project
Tek Tips Forum
ZVON.ORG
TopXML Search