0

How to change the format 24-hour to 12-hour in Delphi 10.3 Rio in DBGridEh field?

I am trying to find one in the property of DBGridEh but no avail. Should I have to create a code for this or an override to system utils?

Kromster
  • 7,181
  • 7
  • 63
  • 111
Mel
  • 101
  • 1
  • 9
  • 1
    You would usually set in for the Field in the DataSet not the Grid. Take a look at : https://stackoverflow.com/questions/46550892/my-time-field-in-my-dbgrid-is-showing-the-date-and-time-instead-of-time-only-d – Brian Sep 26 '19 at 12:14
  • @Brian Thanks for this. But I already did the formating of properties including editmask, I still couldn't get rid of the 24-hour format. I need the 12-hour format instead. My computer is also set to 12-hour format. I am pretty sure that somewhere there is a switch to change it in delphi. – Mel Sep 26 '19 at 12:36
  • 1
    Works fine here using a DisplayFormat like the one suggested by Val Marinov in an answer : `dd/mm/yyyy hh:nn AM/PM`. – Brian Sep 26 '19 at 17:15
  • You don't need the `EditMask`. You need the `DisplayFormat` property for the field itself. Setting it there means that no matter where you display the field value, it will always have the same consistent appearance. But if the user has configured their computer to use 24 hour time formatting, you should respect that setting instead. – Ken White Sep 26 '19 at 23:15

1 Answers1

4

Each column of DbGridEh has property "DisplayFormat". So you can use this property to set desired format acording his data type. For columns with datatypes TDateTime you can use :

dd.mm.yyyy hh.nn AM/PM

or

hh.nn.ss AM/PM

for example to set 12 hour format.

For more information about formatting a DateTime values see:

http://www.delphibasics.co.uk/RTL.asp?Name=formatdatetime

Edit:

As Ken White noted, you can also use TField.DisplayFormat so no matter where the content of the field is displayed, it will have the same format.

Val Marinov
  • 2,705
  • 17
  • 22
  • This is better done at the table level with the `TField.DisplayFormat` property, so that no matter where the field content is shown it will have the same format. (Not downvoting your post, just mentioning that there is a better solution.) – Ken White Sep 26 '19 at 23:15
  • 1
    @KenWhite I agree. I answered a question related to a specific component. By the way, this solution allows the same field to be presented differently in several columns on one grid. I have no idea if it's useful. I added your comment in the answer. – Val Marinov Sep 27 '19 at 07:45
  • @ValMarinov Thank so much. It's more clearer now to me. – Mel Oct 01 '19 at 02:59
  • @KenWhite Thank you for your inputs as well. – Mel Oct 01 '19 at 03:00