Thursday, June 28, 2012

Java Script Tutorial 21 : JavaScript Date Object


The JavaScript Date object is used to work with dates and times. Date objects are created with the Date() constructor. We can initiate date by four ways:

  • new Date()
  • new Date(milliseconds)
  • new Date(dateString)
  • new Date(year, month, day, hours, minutes, seconds, milliseconds)

We can operate a number of methods after creating a date object. These method allow you to get and set value of year, month, day, hour, minute, second, and milliseconds of the object. All dates are calculated in milliseconds from 01 January, 1970 00:00:00 Universal Time (UTC) with a day containing 86,400,000 milliseconds.
For example


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


Set/Manipulate Dates :
We can easily manipulate the date by using the methods available for the Date object. For example


var myDateobj=new Date();
myDateobj.setFullYear(2011,0,15);


Adding days to a date shifts the month or year, the changes are handled automatically by the Date object itself.


Compare Two Dates :
We can also use Date object to compare two dates. For example


var y=new Date();
y.setFullYear(2110,0,16);
var current = new Date();
if  ( y > current )
   {
       alert("Current Date is before 16th January 2110");
   }
 else
   {
       alert("Current Date is after 14th January 2100");
   }

Java Script Tutorial 20 : JavaScript Objects


JavaScript is an Object Based Programming language, and allows you to define your own objects and make your own variable types. 
An object is just a special kind of data. An object has properties and methods. We can create our own objects in java script, which will be discussed in future tutorials. 


Properties :
Properties are the values associated with an object. For example


<script type="text/javascript">
     var txt="This is text.";
     document.write(txt.length);
</script>


In above code, txt is an object and length is its property. Result displayed in browser will be : 13


Methods :
Methods are the actions that can be performed on objects. For example


<script type="text/javascript">
     var txt="This is Text";
     document.write(txt.toUpperCase());
</script>


In above code txt is an object and toUpperCase() is method.  Result displayed in browser will be : 
THIS IS TEXT


Tuesday, June 26, 2012

Java Script Tutorial 19 : JavaScript Special Characters


In JavaScript, Special characters can be added to a text string by using the backslash sign. For example we want to display following text in JavaScript code. 


var txt="This "This is a "big" Apple.";


In JavaScript, a string is started and stopped with either single or double quotes. This means that the string above will be chopped to: 


This is a  


We must place a backslash (\) before each double quote in "big" to solve this problem. This result string will look like this :


var txt="This "This is a \"big\" Apple.";


After executing in browser, it will display string as : 


This is a "big" Apple. 


There are many other special characters that can be added to a text string with the backslash sign, these are :


\'          (  single quote  )
\"         (  double quote  )
\\          (  backslash  )
\n         (  new line  )
\r          (  carriage return  )
\t          (  tab  )
\b         (  backspace)
\f          (  form feed  )

Java Script Tutorial 18 : JavaScript Throw


JavaScript throw statement allows you to create an exception. If you use this statement together with the try .. catch statement, you can control program flow and generate error messages. The exception can be a string, integer, Boolean or an object. Syntax of throw is as:


throw exception


For example


<!DOCTYPE html>
<html>
<body>
<script type="text/javascript">
var x=prompt("Enter a number between 2 and 5:","");
try

       if(x>5)
         {
             throw "Error 1";
         }
       else if(x<2)
         {
             throw "Error 2";
         }
 }
 catch(err)
 {
       if(err=="Error 1")
         {
              document.write("The value is higher then 5.");
         }
       if(err=="Error 2")
         {
              document.write("The value is lower then 2.");
         }  
 }
</script>
</body>
</html>


Java Script Tutorial 17 : JavaScript Try .. Catch

JavaScript try .. catch statement allows you to test a block of code for errors. The try block contains the code to be run, and the catch block contains the code to be executed if an error occurs. Syntax of JS try .. catch is :


try
  {
       // code
  }
catch()
  {
       // Handle errors
  }


For example


<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var txt="";
function Showmessage()
{
     try
       {
            adddlert("JS try .. catch testing ....");
       }
     catch(err)
       {
            txt="There was an error on this page.";
            alert(txt);
       }
}
</script>
</head>
<body>
   <input type="button" value="Click to View message" onclick="Showmessage()" />
</body>
</html>