16. JavaScript Date Object

JavaScript Date Object

Create a Date Object
Set Dates
Compare Two Dates
Examples

Create a Date Object

The Date object is used to work with dates and times

Date objects are created with the Date() constructor.

There are four ways of instantiating a date.

new Date() //current date and time
new Date(milliseconds)//milliseconds since 1970/01/01
new Date(dateString)
new Date(year,month,day,hours,minutes,seconds,milliseconds)

Most of the preceding parameters are optional

when a parameter is not specified, 0 is passed to the method by default.

today=new Date()
d1=new Date("october 13, 1975 11:13:00")
d2=new Date(79,5,24)
d3=new Date(79,5,24,11,33,0)

Set Dates

set the date object to a specific date (14th January 2010);

we set a date object to be five days into the future:

var myDate = new Date();
myDate.setDate(myDate.getDate()+5);

Note: if adding five days to a date shifts the month or year, the changes are handled automatically by the Date object itself!

Compare Two Dates

The Date object is also used to compare two dates.

compares today's date with the 14th January 2010:

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");
}

how to get today's date

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

how to use getTime() to calculate the milliseconds since 1970.

<html>
<body>
<script type="text/javascript">
var d=new Date();
document.write(d.getTime()+" milliseconds since 1970/01/01");
</script>
</body>
</html>

how to use setFullYear() to set a specific date.

<html>
<body>
<script type="text/javascript">
var d=new Date();
d.setFullYear(1992,11,3);
document.write(d);
</script>
</body>
</html>

how to use toUTCString() to convert today's date (according to UTC) to a string

<html>
<body>
<script type="text/javascript">
var d=new Date();
document.write("original form:");
document.write(d+"<br/>");
document.write("to sreing (universal time):");
document.write(d.toUTCString());
</script>
</body>
</html>

the getday() method returns the day of the week as a number, with Sunday = 0.
the following example demonstrates how to use getDay() and an array to display the day of the week as a text string rather than a number.

<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 is" +weekday[d.getDay()]);
</script>
</body>
</html>

how to display a clock on your Web page

<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>

No comments:

My Favorite Books

  • C and Data Structures by Ashok N. kamthane
  • Web Technologies by A. A. Puntambekar
  • Learn HTML and CSS with W3Schools
  • Learn JavaScript and Ajax with W3Schools
  • HTML5 Black Book: Covers Css3, Javascript,XML, XHTML, Ajax, PHP And Jquery
  • HTML5 Application Development Fundamentals: MTA Exam 98-375
  • .NET 4.0 Programming 6-In-1, Black Book
  • SQL Server 2008 R2 Black Book
  • Asp.net 4.0 Projects Covers: net 3.5 And .net 4.0 Codes, Black Book
  • UNIX Shell Programming 1 Edition by Yashavant Kanetkar
  • UNIX and Shell Programming 1 Edition by Richard F. Gilberg, Behrouz A. Forouzan
  • Computer Networks by Andrew S. Tanenbaum
  • Multiple Choice questions in computer science by Timothy J Williams