Home Asp.net How to split string into Array of Day, Month, Year in Asp.net...

How to split string into Array of Day, Month, Year in Asp.net Using C# Vb.net [Solved]

140
1
Redirect Page on Button Click

There are several methods available to split string into array. In my previous tutorials, I’d explained how to show confirm message box using javascript, how to show alert message from code-behind, how to call javascript function from code-behind and other more cracking tutorials on Asp.net, JavaScript, jQuery here.

One of them is using jquery split function but here in this tutorial, I’ll explain how to split string into array date into array of day, month and year from code-behind in asp.net using C# and Vb.net split() method.

If you want to C# string split to an array, it’s an easy to do so. For that, you need to use split function in asp.net C# or vb.net. To use this method it will require separator as a parameter like /, @, – etc as per your choice, use to separate given string.

Here is the code to split string date into specific day, month and year.

Split String Into Array In C# (Day, Month and Year)

Following is the sample code snippet to split string in to date attray as day, month & year using C#:

string strDate = "26/07/2011"; //Format - dd/MM/yyyy
//split string date by separator, here I'm using '/'
string[] arrDate = strDate.Split('/');

//now use array to get specific date object
string day = arrDate[0].ToString();
string month = arrDate[1].ToString();
string year = arrDate[2].ToString();

Split String Date to Array of Day, Month and Year – [Vb.net]

Following is the sample code snippet to split string in to date attray as day, month & year using vb.net:

Dim strDate As String = "26/07/2011" 'Format - dd/MM/yyyy
'split string date by separator, here I'm using '/''
Dim arrDate() As String = strDate.Split("/"c)

'now use array to get specific date object'
Dim day As String = arrDate(0).ToString()
Dim month As String = arrDate(1).ToString()
Dim year As String = arrDate(2).ToString()

Example Result

Here’s how it’ll split the input string in day, month and year separately:

Date: "26/07/2011";day: "26";
month: "07";
year: "2011";

That’s it, this is one of the simplest way to split string into day, month and year easily in .net using c# or vb.net.

Let me know if you’ve any questions or doubts about this tutorial by writing down your queries in comment below. I would be happy to provide you my feedback as soon as possible.

Happy Coding!

Previous articleGet Selected Text Value from DropDownList in Asp.net using JavaScript
Next articleDropDownList Validation: Validate DropDownList using JavaScript in Asp.net
Hi there, I am Mayank, the man behind Technical Mack. I started AspnetO with a motive to educate people on various programming languages ranging from beginners to expert level. Through this blog, I aim to provide more insightful content.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here

3 × four =