0

I have this code storing date time values in data columns:

DataTable apptTable = new DataTable();
apptTable.Columns.Add("Appointment", typeof(DateTime));
apptTable.Columns.Add("Time Scheduled", typeof(DateTime));

This displays the full datetime with milliseconds. enter image description here

How can I hide the milliseconds? I've tried passing the datetime as a formatted string like so: scheduled.ToString("g"); but this only works when the data column is of type string. The only problem with having the column be of type string is that it doesn't sort properly.

I am not trying to format the datetime object itself. I am trying to change how the DateTime is displayed in the DataColumn/Gridview. Even if I format the datetime object to remove milliseconds, it still displays in the table as 00.

Bedir
  • 476
  • 1
  • 6
  • 17
  • https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings – VDWWD Jul 24 '19 at 14:23
  • I am not trying to format the datetime object itself. I am trying to change how the DateTime is displayed in the DataColumn/Gridview. Even if I format the datetime object to remove milliseconds, it still displays in the table as 00. – Bedir Jul 24 '19 at 14:33
  • DateTime does not have a format. You need to specify it when you need that object. – VDWWD Jul 24 '19 at 14:34
  • I am trying to change how it is displayed in the data table. – Bedir Jul 24 '19 at 14:38
  • In any case, this is not a duplicate of the mentioned question. – Bedir Jul 24 '19 at 14:40
  • it is a duplicate. The DataTable hold the DateTime object. Only when you display it (like in a GridView) you have to specify formatting. – VDWWD Jul 24 '19 at 14:41
  • You'll probably end up with something like this: ``. That's how you tell the GridView how to format a DateTime when displaying it. – mason Jul 24 '19 at 15:12
  • Thanks mason, that would've worked if I wasn't adding the datatable to the gridview programmatically/in the code behind not in the asp. – Bedir Jul 24 '19 at 16:44
  • @Bedir How you bind the DataTable isn't really that important. Wherever your columns are declared - whether in the declarative ASPX markup or in the code code behind - you need to make sure you set the correct DataFormatString. – mason Jul 24 '19 at 18:30
  • I ended up using jQuery to remove extra milliseconds: ````$("td").each(function (index) { if ($(this).text().includes(":00 ")) { var currentText = $(this).text(); var replacedText = currentText.replace(":00 ", " "); $(this).text(replacedText); } });```` – Bedir Jul 24 '19 at 20:00

0 Answers0