Home » Study JavaScript, Tech Corner

Study JavaScript Easy Part-4

1 May 2009 No Comments Posted By:LG

Previous
I found a few more things which am posting in here.This is the continuation of JavaScript OBJECTS

Date Objects
These are used to work with date,day,month,year,time etc.

Eg:1(To get the current date)
<html>
<body>
<script type=”text/javascript”>
document.write(“Todays date:-”+Date( ));
</script>
</body>
</html>

Eg:2(To get the day of a week)
<html>
<body>
<script type=”text/javascript”>
var d=new Date();
document.write(“Today it is ” + d.getDay( ));
</script>
</body>
</html>

The above Eg: can be improvised a bit,check the Eg: below.
In Eg:2 the getDay( ) method returns an integer value equivalent to the day,in the Eg:3 it is more specific.

Eg:3
<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>

Eg:4 (Clock)

<html>
<head>
<script type=”text/javascript”>
function showTime()
{
var today=new Date();
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();
m=checkTime(m);
s=checkTime(s);
document.getElementById(‘div1′).innerHTML=h+”:”+m+”:”+s;
t=setTimeout(‘showTime()’,500);
}
function checkTime(i)
{
if (i<10)
{
i=”0″ + i;
}
return i;
}
</script>
</head>

<body onload=”showTime()” >
<div id=”div1″>
</div>
</body>
</html>

Math Objects
Is used for mathematical operations,it includes several mathematical constants and methods.

CONSTANTS:
Math.PI —for the constant 3.14(22/7)
Math.SQRT2—-Square root of 2
Math.LN2——-Natural Log of 2
Math.LN10——Natural Log of 10

METHODS:
Copy the codes to a notepad and save as an html file and see the output.

  • Math.round( )
  • <html>
    <body>
    <script type=”text/javascript”>
    document.write(Math.round(1.60) + “<br />”);
    document.write(Math.round(1.50) + “<br />”);
    document.write(Math.round(1.49) + “<br />”);
    document.write(Math.round(-2.40) + “<br />”);
    document.write(Math.round(-2.60));
    </script>
    </body>
    </html>

  • Math.max( )
  • <html>
    <body>
    <script type=”text/javascript”>
    document.write(Math.max(3,9) + “<br />”);
    document.write(Math.max(-3,6) + “<br />”);
    document.write(Math.max(-1,-8) + “<br />”);
    document.write(Math.max(7.25,7.30));
    </script>
    </body>
    </html>

  • Math.min( )
  • <html>
    <body>
    <script type=”text/javascript”>
    document.write(Math.min(3,4) + “<br />”);
    document.write(Math.min(-2,3) + “<br />”);
    document.write(Math.min(-3,-5) + “<br />”);
    document.write(Math.min(4.25,4.30));
    </script>
    </body>
    </html>

    Goto Previous Post

    Related posts:

    1. Study JavaScript Easy Part-3
    2. Javascript study Part-2
    3. Study Java Script Easy
    4. Simple Cascading Menu using JQuery

    Leave your response!

    Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

    Be nice. Keep it clean. Stay on topic. No spam.

    You can use these tags:
    <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>