I currently have the standard Datetime using AM/PM. I wish to have 24H Display. Searching the web gave me alot of options with String.Format() option.
Problem is that this creates a string object to display the date. I need to use a datetime object because it is used in a table and the date column must be able to be sorted (asc/desc). The table is of a GridView Object.
Code Example
Currently the code shows the date as follows 8/21/2009 11:28:36 AM. I a perfect situation it would show as 11:28:36 21/8/2009. I know i can get that result with the following code.
date.ToString("dd/MM/yyyy HH:mm:ss");
Problem is that this is a string. The string get’s placed into the table and if i sort the date column it will first sort on day instead of year.
showing the followin descending column.
21/8/2009 11:28:36
20/1/2013 15:55:23
To correctly be able to sort the column the inserted objecttype should be DateTime. the object type is forced by the following code.
dateTable.Columns.Add(new DataColumn("Date", typeof(DateTime)));
According to this article you can use a boundfield like so:
You can then simply use this format string:
Where capital H renders the 24-hour time, as opposed to a small h which would render a 12-hour time.