views:

792

answers:

3

Hi,

I have a dataset that is populated by reading excel file. The dataset stores the data from the excel. The date in the dataset in in the format "2\2\2009 12:00:00 AM" but i need the data format converted to "2\2\2009" . I want to change the format of all the data in that particular column. Please help.

+4  A: 

Here's one way:

foreach (DataRow row in yourDataTable)
{
    DateTime dt = DateTime.Parse(row["Date"].ToString());
    row["Date"] = dt.ToShortDateString();
}

This is assuming that the "Date" column is just a text field rather than already a DateTime field.

MusiGenesis
You beat me to the submit button... I had the same answer
Jeff Fritz
I have a datetime column in a dataset and i want to change the format of the data of that column alone
Jebli
Haw! The beauty of not bothering to make sure my code even compiles. =)
MusiGenesis
@Jebli: in that case, you don't need to iterate through the table and change anything. Just use the ToShortDateString() method whenever you need to display the value. Formatting isn't really something you do to a DateTime object - it's something you do when you want to display its value.
MusiGenesis
Hi i tried your code but when the value is stored after formating the dataset to the same dataset again the value is deiplayed as "2/2/2009 12:00:00 AM".Why i am trying to change the format is i am doing a bulk update and it updates the database column in this format.
Jebli
Is the column in your database a Date/Time column (I don't know what kind of db this is)? If so, I don't think you need to change anything, unless you don't want the time part at all. If you want only the date part, you can (depending on what database this is) use a DATE column (as distinct from a column that stores date AND time) instead.
MusiGenesis
A: 

You can customize the output using a pattern if it is a DateTime object to the format you wanted, "2\2\2009".

string output1 = dt.ToString(@"mm\\dd\\yyyy");
string output2 = dt.ToString("mm/dd/yyyy"); //did you mean this?
thomasnguyencom
i want the original format of the data in the dataset to be changed to the format "02/26/2009"
Jebli
A: 

A DateTime value or column has no format. It's just binary data.

Where do you want the formatted output to appear? That's where you should apply a format string. For instance, if you wanted it to appear in a column of an ASP.NET DataGrid, then you would set the DataFormatString property of the BoundColumn to "mm/dd/yyyy".

John Saunders
Hi i am using a bulk upload in that case how do i change the data format in the dataset and assign the value
Jebli
Please show the code you are using for bulk upload. Also, what's the column type in the destination? Nvarchar?
John Saunders
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(destinationConnection.ConnectionString)) { for (int count = 0; dsSourceColumn.Tables[1].Rows.Count > count; count++) { bulkCopy.ColumnMappings.Add("sourcecolumn", "sourcecolumn"); } }In my case both the source and destination coumn names will be same
Jebli
Ouch, my eyes! Edit the question to include that code!
John Saunders
I meant is the destination column datetime or nvarchar?
John Saunders
sorry.The destination column was varchar.
Jebli