I want to dynamically format any column's date to yyyy-MM-dd
. I know i can conditionally set a specific column to this date format, but is there any way to do it dynamically for all columns that have dates. Currently I am using a try catch
here which works for all dates, but the problem is that any columns that aren't dates i.e. a value of 3.8707 will become 01/03/8707
on the csv output
foreach (var array in from DataRow myrow in dt.Rows select row.ItemArray)
{
for (i = 0; i < array.Length; i++)
{
try
{
DateTime date = DateTime.Parse(array[i].ToString());
swOut.Write(date.Year + '-' + date.Month + '-' + date.Day + ",");
}
catch (Exception)
{
swOut.Write(array[i] + ",");
}
}
swOut.WriteLine();
}