Jebli
Jebli

Reputation: 2825

formatting datetime column in a DataSet

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.

Upvotes: 2

Views: 33935

Answers (4)

Ajsatis
Ajsatis

Reputation: 41

Try this. It will work

var result = from a in ds.Tables[0].AsEnumerable() select new[] { Convert.ToDateTime(a[0]).ToString("dd/MM/yyyy") };

Upvotes: 0

John Saunders
John Saunders

Reputation: 161831

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".

Upvotes: 0

tom
tom

Reputation: 357

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?

Upvotes: 1

MusiGenesis
MusiGenesis

Reputation: 75396

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.

Upvotes: 9

Related Questions