Displaying 4 digit years in Delphi applications

By: Borland Staff

Abstract: Delphi displays dates using 2 digit years by default

When date fields are displayed in the data aware controls the year is
always displayed with 2 digits.  How can I display years with 4 digits

The information in this article applies to:
* InterBase Express (IBX)

When connecting to an InterBase database date datatypes are always returned to the client using InterBase's date format:
ISC_QUAD. All other databases have their own way of representing 4 digit years. It is the client's responsibility to display the date information in a
format suitable for the particular application.

When using Delphi with either the BDE or IBX year data is displayed with 2 digits 
by default.  

There are many ways to change this behavior and customize the displayed date
format.  Two such ways will be explained here.

Changing Regional Settings for the System
One way to adjust the display format of date fields is to change the system's settings 
for displaying dates.  In the control panel applet "Regional Settings" there is a Date tab
that allows the system's date format to be customized.  You can change the value for the
combo box "Short date style"

Changing the Short Date Format for the Application
Another way to adjust the display formats for dates is to set the variable ShortDateFormat.
This variable is defined in the Sysutils unit.  You can customized the date display format
for the application by setting this variable.  For more information search on ShortDateFormat
in the Delphi help.

Here is an example that sets the variable to display 4 digit years

procedure TForm1.FormCreate(Sender: TObject);
  ShortDateFormat := 'mm/dd/yyyy';

Changing the Date Display Format for a Particular Field
You can also customize the date display format for individual fields.
This is accomplished by creating a persistent field component for the 
field and setting the DisplayFormat property for the component.

The easiest way to create a persistent field is to use the Fields
editor in Delphi.  After dropping your dataset component on a form, 
right click on the component and select "Fields Editor...".  Right clicking
in the Fields editor brings up a menu which allows you to "Add all fields"

Once the fields are created you can customize the properties for the date field.
Select the date field's Field object from the list.  Then in the object inspector you can
set the DisplayFormat property for the date field.  You can also change the property in
code as demonstrated in the following snippet of code.  

Note: Query1HIRE_DATE is the field component that was created from the Fields editor.

procedure TForm1.FormCreate(Sender: TObject);
  Query1HIRE_DATE.DisplayFormat := 'mm-dd-yyyy';

Server Response from: ETNASC03