8

Consider i have a datatable dt and it has a column DateofOrder,

DateofOrder
07/01/2010
07/05/2010
07/06/2010

I want to format these dates in DateOfOrder column to this

DateofOrder
01/Jul/2010
05/Jul/2010
06/Jul/2010

Any suggestion..

John Saunders
  • 160,644
  • 26
  • 247
  • 397
ACP
  • 34,682
  • 100
  • 231
  • 371

4 Answers4

13

The smartest thing to do would be to make sure your DataTable is typed, and this column is of type DateTime. Then when you go to actually print the values to the screen, you can set the format at that point without mucking with the underlying data.

If that's not feasible, here's an extension method I use often:

public static void Convert<T>(this DataColumn column, Func<object, T> conversion)
{
    foreach(DataRow row in column.Table.Rows)
    {
        row[column] = conversion(row[column]);
    }
}

You could use in your situation like:

myTable.Columns["DateOfOrder"].Convert(
    val => DateTime.Parse(val.ToString()).ToString("dd/MMM/yyyy"));

It only works on untyped DataTables (e.g. the column type needs to be object, or possibly string).

Rex M
  • 142,167
  • 33
  • 283
  • 313
  • i used your second option but `Convert` is not a method on DataColumn of a datatable.. – ACP Jul 09 '10 at 04:45
  • That's why he has the extension method. You need to add the first method he uses to your program, and then you should be able to use convert. – fire.eagle Jul 09 '10 at 13:10
  • @Pandiya if you'll note the bold part of the answer, it is an *extension method* you have to add, which is why I wrote it here. – Rex M Jul 09 '10 at 13:58
  • Also, note you need to convert BEFORE populating your DataTable, otherwise this doesn't do much. – Rob Koch Jun 05 '13 at 17:03
  • Different format customization will working as in another thread, http://stackoverflow.com/questions/28523515/how-to-change-date-format-for-the-date-column-in-datatable – Suriya Aug 08 '15 at 09:43
0

.ToString("dd/MMM/yyyy") (assuming your data is DateTime type)

saille
  • 9,014
  • 5
  • 45
  • 57
0

Just expounding on saille's answer here:

For a DateTime, format isn't an issue. A DateTime is actually the number of ticks counting up from midnight, January 1, 1 A.D. So, really, it's just a long. Formatting only becomes an issue when it comes time to convert it into a string. So, you'll have to go and take care of the formatting either when you pull it out of the data table and are ready to output it, or put it into the data table as a string (which I would not recommend, for flexibility purposes). The formatting can be done with the .ToString call on the DateTime that saille suggests, .ToString("dd/MMM/yyyy")

fire.eagle
  • 2,723
  • 1
  • 21
  • 23
0

Just to add, you need to put your extension method to a .cs file usually some sort of a Utility.cs and then get access to that with the help of using statement.

A complete example is provided here: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods

asiby
  • 3,229
  • 29
  • 32