Click here to Skip to main content
15,314,664 members
Please Sign up or sign in to vote.
1.40/5 (2 votes)
See more:
Im having datatable with multiple date columns. While exporting to excel im getting error that dateformat is wrong. I need to convert those data column to string in datatable. can anyone suggest pls.

What I have tried:

DataTable dt = dv.ToTable();
           foreach (DataRow dr in dt.Rows)
           {

           }
Posted
Updated 4-Jun-18 3:05am
Comments
Richard MacCutchan 3-Jun-18 3:41am
   
Excel use its own format for storing Dates and Times, which Google will find for you.

As to me - your problem is not related with date column, but with the method you use to export data to MS Excel file. Because you didn't show us your code and sample data you're trying to export to Excel, we aren't able to help you.
Seems you're storing dates as string, that's why you're facing with wrong format of date.
   
You can try this, assuming your column name is Date

C#
foreach (DataRow row in dt.Rows)
{
    DateTime dt = DateTime.Parse(row["Date"].ToString());
    row["Date"] = dt.ToShortDateString();
}
   
Comments
Richard MacCutchan 3-Jun-18 3:40am
   
Why are you converting a DateTime to a string just so you can parse it back to a DateTime in order to convert it back to a string?
sajeetharan 3-Jun-18 6:50am
   
what do you mean?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900