C# WPF – Convert string to Datetime Format

MiniBytes: In this article we are going to see, how to create a convertĀ  string to Datetime Format in C#. This tutorial will be helpful for you to create a Datetime formats from string which you can use in your C# Form application or WPF application. What i am narrating here is the code which was taken from the live projects which i am currently working on.

In this tutorial we are going to see how to convert the string to Datatime formatted variable. Here instead of using the simple string, i am going to use the string which was fetched from the DataRowView from a DataGrid.

Here is the code which was used to fetch the data from the DataRowView from a DataGrid.

Code for DataRowView:

DataRowView drv = (DataRowView)lstView.SelectedItem;
string ddate = drv[0].ToString();

In this code, we are getting a selected item from the datagrid and assigning it to a string variable.

Now using the Convert.ToDateTime() function, we are going to convert the normal string into the DataTime format using he below code.

txtBox1.Text = Convert.ToDateTime(ddate).ToString("dd-MM-yyyy HH:mm:ss");

So the txtBox1.Text will get the DataTime formatted string, which you can use to display your “form application” or “wpf application”.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *