Get IndexOf string in Javascript

string indexof in javascript
In JavaScript strings IndexOf() function is used to find position of specified character in the string. When IndexOf() method is used in javascript it returns position of first occurrence of the given character. If specified character is not found in string then it returns –1 value.

Syntax for IndexOf()


string.indexOf(searchstring, start)

Here search string is the character that is to be find and start is the position from which search to begin.

Example of IndexOf() Method [JavaScript]:


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function GetStringIndex() { 
var str="Javascript IndexOf Method.";
document.write( "a is at position"+ str.indexOf("a") + "<br />");
document.write("IndexOf is at position"+ str.indexOf("IndexOf") + "<br />");
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" value="Submit" onclick="return GetStringIndex();" />
</div>
</form>
</body>
</html>

//Output<
//a is at position 1
//IndexOf is at position 11

Split string JavaScript

split string in javascript
In JavaScript we need to split the string to perform some operations on string. JavaScript uses Split() method to split the given string in JavaScript. Split() method basically split string into array of substrings and generates new array.

String split Syntax:


string.split(separator, limit)

Here separator is a character by which we want to separate string. and limit is to give up to how many number split the string.

Sample to split string [JavaScript]


<script type="text/javascript">
 function splitString()
 {
  var str="Split Javascript String";
  document.write(str.split(" "));
 }
</script> 


//Output
//Split, Javascript, String

In above example of string split, we give space(“ ”) character to split the string with. so it splits the string with “ “ and returns array with substrings.

Get GridView SelectedIndexChanged event asp.net

gridview selectedindexchanged event
In this post we will see how to use SelectedIndexChanged Event of GridView in ASP.Net. SelectedIndexChanged event get fired when we click select button of the gridview row. We can use this event to perform some operation on form when row is selected like call custom routines, change labels, change titles..etc.

Sample code to see how SelectedIndexChanged event in gridview works [ASP.Net, C#]:


<asp:GridView ID ="GridView1" runat="server" 
onselectedindexchanged="GridView1_SelectedIndexChanged" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1" AutoGenerateSelectButton="true">

<Columns>

<asp:BoundFieldDataField="ID"HeaderText="ID"SortExpression="ID" />

<asp:BoundFieldDataField="Name"HeaderText="Name"SortExpression="Name" />

<asp:BoundFieldDataField="Address"HeaderText="Address" SortExpression="Address" />

</Columns>

</asp:GridView>

<asp:SqlDataSourceID="SqlDataSource1"runat="server" ConnectionString="<
;%$ ConnectionStrings:PersonConnectionString %>" 
SelectCommand="SELECT * FROM [PersonInfo]"></asp:SqlDataSource>
</p>

<p>
<asp:labelid="lblMessage"forecolor="Red"runat="server"/>
</p>



Code behind for selectedIndexChanged Event:






protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)

{
 
  GridViewRow row = GridView1.SelectedRow;
  
  lblMessage.Text = "Selected" + row.Cells[2].Text + ".";
}

Get Time part from datetime C#

Get time part from datetime

[C#] DateTime Format is often needed while coding in C#.net. Datetime formats are basically used for converting Date and Time into Specified String representation of DateTime.

In this post of C# Datetime Format, we will see how to get the time factor out of the DateTime. We can get Hour, Minute and Second from the DateTime. DateTime provides Properties for getting Hour, Minute and Second.

Sample code to get Hours, Minutes and Seconds from Date [C#]:

class Program  
{
 static void Main(string[] args)
  {
   DateTime date = DateTime.Now;
   Console.WriteLine("Hours= " + date.Hour);
   Console.WriteLine("Minutes= " + date.Minute);
   Console.WriteLine("Seconds= " + date.Second);
  }
}

Output of this program will show current Hours, Minutes and Seconds.

Datetime compare

compare datetime
Using C# Datetime, we can compare two dates. C# DateTime class provides Date.Compare method to compare dates. DateTime.Compare() method compares dates and returns an integer value. Using that integer value we can compare date whether date is less, or equal or greater that second date.

C# Datetime Compare Syntax:

public static int Compare(DateTime dt1,DateTime dt2)

Sample code to compare dates using DateTime.Compare() [C#]

public class StringToDateTime
    {
        public static void Main()
        {
            DateTime dt = new DateTime(2012, 1, 16);
            DateTime dt1 = new DateTime(2012, 1, 16);
            int result = DateTime.Compare(dt, dt1);

            if (result == 0)
             Console.WriteLine("Date is same as today's date");
        }
    }

//Output
//Date is same as today's date

Get DateTime Difference C#

Get DateTime difference

In [C#] DateTime Formats, C# Datetime provides verious ways to get the difference between dates. Using C# Datetime we can get difference betwwen two dates, we can get difference between days, difference between hours, difference between Minutes, difference between seconds.

DateTime class provides subtract method to get the difference between two dates.And using System.TimeSpan we can show the difference. We will see C# code that show how to use Subtract method and System.TimeSpan to calculate difference between two dates.

[C#] Example to get difference between two dates:

using System;
    namespace DateAndTimeDifferenceSample
    {
        class Program
        {
            static void Main(string[] args)
            {
                DateTime fromdt = new DateTime(2012, 1, 16);
                DateTime todt = new DateTime(2012, 2, 17);
                TimeSpan dtDiff = todt.Subtract(fromdt);
                Console.WriteLine(dtDiff);
            }
        }
    } 

//Output wiil show difference in days like below
//32.00:00:00

Using C# Datetime we can also get the difference in Hours, Minutes, And Seconds. Let’s see the C# example that show how to calculate difference in Hr, Mins, and in Secs.

Example to get difference for Hours, Minutes, and Seconds [C#]:
class Program
    {
        static void Main(string[] args)
        {
            DateTime fromdt = new DateTime(2012, 1, 16, 22, 01, 55);
            DateTime todt = new DateTime(2012, 2, 17, 23, 15, 00);
            TimeSpan dtDiff = todt.Subtract(fromdt);
            Console.WriteLine("Hours" + dtDiff.Hours);
            Console.WriteLine("Minutes" + dtDiff.Minutes);
            Console.WriteLine("Seconds" + dtDiff.Seconds);
        }
    }

//OutPut
//Hours= 1
//Minutes= 13
//Seconds = 5

Parse date in C#

c# parse date

In this post we will look how the string with date and time to be converted to DateTime. DateTime class provides Parse() method that takes string parameter that contains date and time. We can convert below kind of string to DateTime using DateTime.Parse() method: DateTime.Parse() method converts given string representation of date and time to it’s DateTime equivalent.


Sample code to convert string to DateTime using DateTime.Parse() [C#]


using System;
 public class StringToDateTime
 {
   public static void Main()
   {
     string myDateTimeValue = "2012-01-16 15:02:26";
     DateTime myDateTime = DateTime.Parse(myDateTimeValue);
     Console.WriteLine(myDateTime);
   }
 }


//Output
//16/01/2012 15:02:26