Convert DateTime data to String in dataset
I have a dataset that stores dates in a DataColumn (the data type of this column is DateTime). I need to change the format from DateTime to a string that will give me the date according to my current culture.
In the case of a single DateTime variable, I can use the overloaded ToString () to achieve this like this:
DateTime.Now.ToString(System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern)
But I need to convert data for all rows in my DataSet to string. Does anyone have any idea how I can achieve this?
Thanks.
a source to share
New answer :)
I had googling came up with this site: Linq to Dataset
This example shows how you can use linq on a dataset, I assume you can use a query to transform the data and place it on a new datatable. Not sure if it will perform better than a loop
a source to share
If I were you, I would store the data in the DataTable as it is. This is, after all, meaningful data that would be meaningless if converted. I would only make any desired changes when displaying / rendering the data, because that implicitly requires converting the data to strings (with additional formatting).
Any DateTime variable can be displayed as a string (culture sensitive) using simple code:
DateTime.Now.ToShortDateString();
// Or
DateTime.Now.ToString("d");
This function already uses formatting information from the current culture.
I would suggest that you take another look at your scenario to assess if you really need to change the datatype of the data in the dataset itself. (or provide us with more information)
a source to share