Setting the BDE Date Format from a Delphi Application

Abstract: Steve Koterski shows Delphi code for setting the BDE date format

Setting the BDE Date Format from a Delphi Application

By Steve Koterski

The Borland Database Engine (BDE) API function DbiSetDateFormat can be used to set the format of dates (the date portion of DATE and TIMESTAMP literals, as seen by the BDE). DbiSetDateFormat does this with values corresponding to the same parameters used in the BDE Administrator for date formats. When calling this function its effects only last as long as the BDE session (i.e., however long the application is running). That is, it has no persistent effect on the BDE configuration. The corollary function is DbiGetDateFormat, which retrieves the current settings.

Both functions require a single argument: a FMTDate structure (a record in Delphi parlance). The FMTDate structure has six fields, each of which control aspect of date formats. These fields are szDateSeparator, iDateMode, bFourDigitYear, bYearBiased, bMonthLeadingZero, and bDayLeadingZero.

The szDateSeparator field is the single character used to separate the year, month, and day portions of a date. For example, in the date "12/30/1957" the forward slash ("/") is the date separator.

iDateMode specifies the order in which the year, month, and day portions of the date appear in the literal. The date literal "12/30/1957" is an example of mdy, "30/12/1957" dmy, and "1957/12/30" is ymd.

bFourDigitYear is a Boolean value that specifies whether to display four digits for the year part of the date (with a True value in this field) or only two digits (a False value).

bYearBiased tells the BDE application whether or not it should add the century to years entered as two digits. See the BDE Administrator online help for more information on this parameter (YEARBIASED).

bMonthLeadingZero is a Boolean value that specifies whether to left pad single-digit months with a zero (for a True value) or only the single digit (for a False value).

bDayLeadingZero is a Boolean value that specifies whether to left pad single-digit days with a zero (for a True value) or only the single digit (for a False value).

Here is an example showing how to use DbiGetDateFormat to get and display the current settings:


  procedure TForm1.SpeedButton1Click(Sender: TObject);
  const
    YesNo: array [False..True] of String = ('No', 'Yes');
    DateMode: array [0..2] of String = ('mdy', 'dmy', 'ymd');
  var
    DateParams: FMTDate;
  begin
    DbiGetDateFormat(DateParams);
    with DateParams do begin
      ShowMessage(
        'Separator: ' + szDateSeparator + #10 +
        'Date mode: ' + IntToStr(iDateMode) +
          ' (' + DateMode[iDateMode] + ')' + #10 +
        'Four digit year: ' + YesNo[bFourDigitYear] + #10 +
        'Year biased: ' + YesNo[bYearBiased] + #10 +
        'Month leading zero: ' + YesNo[bMonthLeadingZero] + #10 +
        'Day leading zero: ' + YesNo[bDayLeadingZero] + #10 +
        'ShortDateFormat: ' + ShortDateFormat
      );
    end;
  end;

The YesNo constant is merely a quick, single-line means for translating a Boolean value to String. ModeToStr is an array constant to translate the three BDE constants for the iDateMode field into plain language text for inclusion in a string.

Here is an example showing how to use DbiSetDateFormat to set the BDE date format parameters for the current application:


  procedure TForm1.SetFormatButtonClick(Sender: TObject);
  var
    DateParams: FMTDate;
  begin
    with DateParams do begin
      StrPCopy(szDateSeparator, Edit1.Text[1]);
      iDateMode := SmallInt(ComboBox1.ItemIndex);
      bFourDigitYear := CheckBox1.Checked;
      bYearBiased := CheckBox2.Checked;
      bMonthLeadingZero := CheckBox3.Checked;
      bDayLeadingZero := CheckBox4.Checked;
      BDE2Delphi(DateParams);
    end;
    Check(DbiSetDateFormat(DateParams));
    DBGrid1.Refresh;
  end;

And here is a small routine to synchronize the Delphi date format settings with the BDE date format settings:


  procedure BDE2Delphi(Params: FMTDate);
  var
    dd, mm, yy: String;
  begin
    with Params do begin
      if bDayLeadingZero then
        dd := 'dd'
      else
        dd := 'd';
      if bMonthLeadingZero then
        mm := 'mm'
      else
        mm := 'm';
      if bFourDigitYear then
        yy := 'yyyy'
      else
        yy := 'yy';
     case iDateMode of
        0: ShortDateFormat := mm + szDateSeparator + dd +
          szDateSeparator + yy;
        1: ShortDateFormat := dd + szDateSeparator + mm +
          szDateSeparator + yy;
        2: ShortDateFormat := yy + szDateSeparator + mm +
          szDateSeparator + dd;
      end;
    end;
  end;

Server Response from: SC2