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 Advanced
JS String Object
JS Array Object
JS Date Object
JS Math Object
JS Window
JS Form
JS Browser

Examples/Quiz
JS Examples
JS Quiz Test

References
JS Objects

Resources
JS Books

JavaScript Date Object


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 Most Common Methods

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 the Date object (four digits) 4.0 4.0 1.0
getHours() Returns the hour of the Date object (from 0-23) 2.0 3.0 1.0
getMinutes() Returns the minute of the Date object (from 0-59) 2.0 3.0 1.0
getSeconds() Returns the second of the Date object (from 0-59) 2.0 3.0 1.0

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.

<html>
<body>
<script type="text/javascript">
var d = new Date()
document.write(d.getDate())
document.write(".")
document.write(d.getMonth() + 1)
document.write(".")
document.write(d.getFullYear())
</script>
</body>
</html>

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

<html>
<body>
<script type="text/javascript">
var d = new Date()
document.write(d.getHours())
document.write(".")
document.write(d.getMinutes() + 1)
document.write(".")
document.write(d.getSeconds())
</script>
</body>
</html>

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.

<html>
<body>
<script type="text/javascript">
var d = new Date()
d.setFullYear("1990")
document.write(".")
</script>
</body>
</html>

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

<html>
<body>
<script type="text/javascript">
var d = new Date()
document.write(d.getUTCHours())
document.write(".")
document.write(d.getUTCMinutes() + 1)
document.write(".")
document.write(d.getUTCSeconds())
</script>
</body>
</html>

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.

<html>
<body>
<script type="text/javascript">
var d = new Date()
var weekday=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
document.write("Today is " + weekday[d.getDay()])
</script>
</body>
</html>

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

<html>
<body>
<script type="text/javascript">
var d = new Date()
var weekday=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
var monthname=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
document.write(weekday[d.getDay()] + " ")
document.write(d.getDate() + ". ")
document.write(monthname[d.getMonth()] + " ")
document.write(d.getFullYear())
</script>
</body>
</html>

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.

<html>
<body>
<script type="text/javascript">
var timer = null

function stop()
{
clearTimeout(timer)
}

function start()
{
var time = new Date()
var hours = time.getHours()
minutes=((minutes < 10) ? "0" : "") + minutes
var seconds = time.getSeconds()
seconds=((seconds < 10) ? "0" : "") + seconds
var clock = hours + ":" + minutes + ":" + seconds
document.forms[0].display.value = clock
timer = setTimeout("start()",1000)
}
</script>
</body>
</html>