views:

654

answers:

1

I have bound fields in GridView to some dateTime property of data objects. The fields are displayed to the seconds level. It looks like 2009-2-3 18:00:00 PM.

I just want it displayed to the minutes. E.g 2009-2-3 18:00.

What is the best way to do this?

Edit:

I find use DataFormatString="{0:g}" is fine for me. The format is 2009-2-3 6:00 PM.

+1  A: 

Although you have found a solution, here's some additional information:

You can find a list of all standard date and time format strings (such as "g") on this page in MSDN: Standard DateTime Format Strings.

And if do not want to use one of the predefined formats, there is also a page about Custom Date and Time Format Strings. For example, to get a date exactly in the format you used in your question ("2009-2-3 18:00") you could use the format string "yyyy-M-d HH:mm".

Before using a custom format string, remember that using the standard (predefined) format strings has the advantage that it automatically takes into account the current culture (regional settings). So if you want to display the date to users, probably a predefined format will be the better choice.

M4N
I tried this "yyyyMMDD HH:mm", but it is incorrect. The time 2009-2-3 6:00:00 PM is trimmed to 2009-2-3 6:00, so I was confused and asked for help
Ken Yao