Study JavaScript Easy Part-4
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.
<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>
<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>
<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:








Leave your response!