The Date object is used to work with dates and times. 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 time than the one created with your own computer.
Now let us see how to use the Date() method to get today's date.
Now let us see how to Use getTime() to calculate the years since 1970.
Now let us see how to Use getDay() and will get Today’s week day.
Now let us see how to how to display a clock on your web page.
The Date object is used to work with date and time. We define a Date object with the new keyword. The following code line defines a Date object called myDate:
var myDate=new Date()
The Date object will automatically hold the current date and time as its initial value!We can easily manipulate the date by using the methods available for the Date object.
In the example below we set a Date object to a specific date (14th January 2010):
var myDate=new Date()
myDate.setFullYear(2010,0,14)
And in the following example we set a Date object to be 5 days into the future:
var myDate=new Date()
myDate.setDate(myDate.getDate()+5)
If adding five days to a date shifts the month or year, the changes are handled automatically by the Date object itself!The Date object is also used to compare two dates.
The following example compares today's date with the 14th January 2010:
Now let us see how to use the Date() method to get today's date.
HTML Code:
<html> <body> <script type="text/javascript">document.write(Date())</script> </body> </html>
HTML Code:
<html> <body> <script type="text/javascript"> var min = 1000*60 var hrs = min*60 var days = hrs*24 var yrs = days*365 var d = new Date() var t = d.getTime() var y = t/yrs document.write("It's been: " + y + " yrs since 1970/01/01!") </script> </body> </html>
HTML Code:
<html> <body> <script type="text/javascript"> var d=new Date() var weekday=new Array(7) weekday[0]="Sunday" weekday[1]="Monday" weekday[2]="Tuesday" weekday[3]="Wednesday" weekday[4]="Thursday" weekday[5]="Friday" weekday[6]="Saturday" document.write("Today it is " + weekday[d.getDay()]) </script> </body> </html>
HTML Code:
<html> <head> <script type="text/javascript"> function startTime() { var today=new Date() var h=today.getHours() var m=today.getMinutes() var s=today.getSeconds() // add a zero in front of numbers<10 m=checkTime(m) s=checkTime(s) document.getElementById('txt').innerHTML=h+":"+m+":"+s t=setTimeout('startTime()',500) } function checkTime(i) { if (i<10) {i="0" + i} return i } </script> </head> <body onload="startTime()"> <div id="txt"></div> </body> </html>
Defining Dates
The Date object is used to work with date and time. We define a Date object with the new keyword. The following code line defines a Date object called myDate:
var myDate=new Date()
The Date object will automatically hold the current date and time as its initial value!We can easily manipulate the date by using the methods available for the Date object.
In the example below we set a Date object to a specific date (14th January 2010):
var myDate=new Date()
myDate.setFullYear(2010,0,14)
And in the following example we set a Date object to be 5 days into the future:
var myDate=new Date()
myDate.setDate(myDate.getDate()+5)
If adding five days to a date shifts the month or year, the changes are handled automatically by the Date object itself!The Date object is also used to compare two dates.
The following example compares today's date with the 14th January 2010:
HTML Code:
var myDate=new Date()
myDate.setFullYear(2010,0,14)
var today = new Date()
if (myDate>today)
alert("Today is before 14th January 2010")
else
alert("Today is after 14th January 2010")


