Javascript Tutorial Example
Example
Javascript Array
JavaScript Boolean
JavaScript Date
JavaScript Math
JavaScript Number
JavaScript String
Javascript Tutorial
Javascript Introduction
Javascript Syntax
Javascript Enable
Javascript Location
Javascript External
Javascript Operators
Javascript Variables
Javascript Functions
Javascript Events
Javascript Statements
Javascript If
Javascript Else If
Javascript While
Javascript For Loop
Javascript Comments
Javascript Array
Javascript Alert
Javascript Confirm
Javascript Prompt
Javascript Print
Javascript Redirect
Javascript Pop Up
Javascript Date
Javascript Form
Javascript Void 0
JavaScript Comparison
JavaScript Switch
JavaScript Try Catch
JavaScript Special Text
JavaScript Throw
JavaScript Math
JavaScript RegExp
Javascript String
Javascript Strings
Javascript Length
Javascript Split
Javascript Search
Javascript Replace
Javascript IndexOf
Javascript Compare
Javascript Advanced
Javascript getElementById
Javascript innerHTML
JavaScript Browser
JavaScript Cookies
JavaScript Animation
JavaScript Image Maps
JavaScript Timing
JavaScript Create Objects
JavaScript Summary
The Date object is useful when you want to display a date or use a timestamp in some sort of calculation. In Java, you can either make a Date object by supplying the date of your choice, or you can let JavaScript create a Date object based on your visitor's system clock. It is usually best to let JavaScript simply use the system clock.
When creating a Date object based on the computer's (not web server's!) internal clock, it is important to note that if someone's clock is off by a few hours or they are in a different time zone, then the Date object will create a different times from the one created on your own computer.
To warm up our JavaScript Date object skills, let's do something easy. If you do not supply any arguments to the Date constructor (this makes the Date object) then it will create a Date object based on the visitor's internal clock.
<h4>It is now
<script type="text/javascript">
<!--
var currentTime = new Date()
//-->
</script>
</h4>
Nothing shows up! That's because we still don't know the methods of the Date object that let us get the information we need (i.e. Day, Month, Hour, etc).
The Date object has been created, and now we have a variable that holds the current date! To get the information we need to print out, we have to utilize some or all of the following functions:
Now we can print out the date information. We will be using the getDate, getMonth, and getFullYear methods in this example.
<h4>It is now
<script type="text/javascript">
<!--
var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
document.write(month + "/" + day + "/" + year)
//-->
</script>
</h4>
Notice that we added 1 to the month variable to correct the problem with January being 0 and December being 11. After adding 1, January will be 1, and December will be 12.
Now, instead of displaying the date we, will display the format you might see on a typical digital clock -- HH:MM AM/PM (H = Hour, M = Minute).
<h4>It is now
<script type="text/javascript">
<!--
var currentTime = new Date()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()
if (minutes < 10){
minutes = "0" + minutes
}
document.write(hours + ":" + minutes + " ")
if(hours > 11){
document.write("PM")
} else {
document.write("AM")
}
//-->
</script>
</h4>
Above, we check to see if either the hours or minutes variable is less than 10. If it is, then we need to add a zero to the beginning of minutes. This is not necessary, but if it is 1:01 AM, our clock would output "1:1 AM", which doesn't look very nice at all!

