Wednesday, 13 October 2010

How to Get Date and Day From Date using C#

Steps to Get Day and Date:

1.  Use dd to get Date

2. Use ddd to get day name in short

3. Use dddd to get Day name in long

C# Code to Print Day and Date values:

using System;
using System.Text;

namespace TestConsole
{
    class DateFormat
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Current Date and Time:");
            Console.WriteLine(System.DateTime.Now);
            Console.WriteLine("Date: {0:dd}",System.DateTime.Now);
            Console.WriteLine("Day: {0:ddd}", System.DateTime.Now);
            Console.WriteLine("Day: {0:dddd}", System.DateTime.Now); 
            Console.ReadLine();
        }
    }
}

How to Get Date and Day From Date using C#

How to Get System Date and Time Using C#

1. Use System.DateTime to get system date and time .

2. Use System.DateTime.Now to get full date and time.

Code to Get Current system date and time:

using System;
using System.Text;

namespace TestConsole
{
    class SystemDate
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Current Date and Time:");
            Console.WriteLine(System.DateTime.Now);        
            Console.ReadLine();
        }
    }
}

Output:

image

C# Code to Print Maximum Two Decimal Places

Steps to print maximum two decimal places:

1. Use {0:0.##} to print two or less than two decimal places.

2. Either use with String.Format() or WriteLine().

C# Code for Max Two Decimal Places:

using System;
using System.Text;

namespace TestConsole
{
    class MaxTwoDecimal
    {
        static void Main(string[] args)
        {
            Console.WriteLine(String.Format("{0:0.##}", 2.6243));
            Console.WriteLine(String.Format("{0:0.##}", -7353.20));
            //or
            Console.WriteLine("{0:0.##}", 5010);
            Console.ReadLine();
        }
    }
}

 

Output:

C# Code to Print Maximum Two Decimal Places

Exponential Number Formatting using C#

Steps Involved:

1. Use {0:e} to print exponential number.

2. Either use with WriteLine() or String.Format().

C# Program to Exponential Number:

using System;
using System.Text;

namespace TestConsole
{
    class ExponentialFormat
    {
        static void Main(string[] args)
        {
            Console.WriteLine(String.Format("{0:e}", 1.3456));
            Console.WriteLine(String.Format("{0:e}", -123.123));
            //or
            Console.WriteLine("{0:e}", 1000);

            Console.ReadLine();
        }
    }
}

Output of Exponential number printing:

Exponential Number Formatting using C#

How to Print a Number in Decimal using C#

Steps to develop a program :

1. Use {0:d} to print decimal number.

2. Either use with String.Format() or WriteLine().

Decimal number Printing in C#:

using System;
using System.Text;

namespace TestConsole
{
    class DecimalFormat
    {
        static void Main(string[] args)
        {
            Console.WriteLine(String.Format("{0:d}", 254));
            Console.WriteLine(String.Format("{0:d}", -783));
            //error
            //Console.WriteLine(String.Format("{0:d}", 193.0));
            //or
            Console.WriteLine("{0:d}", 1000);

            Console.ReadLine();
        }
    }
}

Output:

How to Print a Number in Decimal using C#

How to Format Number to Two Decimal places using C#

Programming Steps:

1. Either you can use String.Format() function to format in given format.

2. Or you can directly use Console.WriteLine("{0:0.00}", 100.0) to print number in a format.

Output of program:

How to Format Number to two Decimal places

Code for Formatting number to two decimal places:

using System;
using System.Text;

namespace TestConsole
{
    class DecimalFormat
    {
        static void Main(string[] args)
        {
            Console.WriteLine(String.Format("{0:0.00}", 163.48967));
            Console.WriteLine(String.Format("{0:0.00}", 173.4));
            Console.WriteLine(String.Format("{0:0.00}", 193.0));

            //or
            Console.WriteLine("{0:0.00}", 100.0);

            Console.ReadLine();
        }
    }
}

Tuesday, 12 October 2010

How to Print Number in Currency Format using C#

Steps for this Program:

1. Use {0:C} to print double number in currency format.

2. This is applicable for all C# projects , either web or windows.

Program to Print values in Currency format:

using System;
using System.Collections.Generic;
using System.Text;

namespace TestConsole
{
    class CurrencyFormat
    {
        static void Main(string[] args)
        {
            double myCurrency = 123456.9854;

            Console.WriteLine("The given number is: {0}", myCurrency);
            Console.WriteLine("The given number in currency format is: {0:C}", myCurrency);
            Console.ReadLine();
        }
    }
}

Output of Currency Format is:

How Print Number in Currency Format using C#

How to use JavaScript onerror Event

Steps to write JavaScript Program:

1. Write a JavaScript function  (Errorhandling) to handle the error.

2. Write onerror=Errorhandling; to pass the control whenever there is error.

Code to use onerror event:

<html>
<head>
<script type="text/javascript">
onerror=Errorhandling;
var My_Error="";

function Errorhandling(msg,url,l)
{
My_Error="Error Details:\n\n";
My_Error+="Error: " + msg + "\n";
My_Error+="URL: " + url + "\n";
My_Error+="Line: " + l + "\n\n";

document.write(My_Error);
}

function Click()
{
adddlert("Error Handling!");
}
</script>
</head>

<body>
<input type="button" value="Click" onclick="Click()" />
</body>

</html>

Output:

How to use JavaScript onerror Event

How to Run this program:

1. Copy the code above and paste to notepad file

2. Save file as SomeFileName.html (Please note that give file extension to .html or .htm)

3. Open this file in Internet explorer.

How to Handle Error in JavaScript | Try Catch Block

JavaScript Coding Steps for Error Handling:

1. Use try block outside the code and handle errors in catch block as given below.

2. You can log the error for debugging purpose.

Try Catch Blocks in JavaScript:

<html>
<head>
<script type="text/javascript">
var My_Error="";
try
  {
  adddlert("This is JavaScript Try Catch Demo!");
  }
catch(ex)
  {
  My_Error="You have reached to catch block.\n\n";
  My_Error+="Error description: " + ex.description + "\n\n";
  alert(My_Error);
  }
</script>
</head>
</html>

Try Catch block Output:

How to Handle Error in JavaScript | Try Catch Block

How to Run this program:

1. Copy the code above and paste to notepad file

2. Save file as SomeFileName.html (Please note that give file extension to .html or .htm)

3. Open this file in Internet explorer.

How to Write First JavaScript Program

Coding Steps:

1.  you need to write your JavaScript code inside the script blocks

<script type="text/javascript"> and </script>

2. use JavaScript function document.write() to print any value on screen. pass value inside the function.

First JavaScript program Code:

<html>
<body>
<script type="text/javascript">
document.write("My First JavaScript Program.!");

</script>
</body>
</html>

Output of JavaScript program:

How to Write First JavaScript Program

How to Run this program:

1. Copy the code above and paste to notepad file

2. Save file as SomeFileName.html (Please note that give file extension to .html or .htm)

3. Open this file in Internet explorer.