TsDateEdit date format

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #71081
    Lasse
    Participant

      I see that UpdateFormat is setting the format by calling DefDateFormat function:

      procedure TsCustomDateEdit.UpdateFormat;
      begin
        FDateFormat := DefDateFormat(FourDigitYear);
      end;

      That function is getting the format from default Windows format settings as you can see here:

      function DefDateFormat(NormalYears: Boolean): string;
      var
        Y: ShortString;
      begin
        Y := iff(NormalYears, 'YYYY', 'YY');
        case GetDateOrder({$IFDEF DELPHI_XE}FormatSettings.{$ENDIF}ShortDateFormat) of
          doMDY: Result := 'MM/DD/' + Y;
          doDMY: Result := 'DD/MM/' + Y;
          doYMD: Result := Y + '/MM/DD';
        end
      end;

      So, if you want to use different date format than your Windows settings you need to modify that function or make UpdateFormat virtual (it is protected but not virtual) and inherit the control. You need to also add DateFormat property, it seems to be private.

      TsCustomDateEdit = class(TsCustomComboEdit)
      protected
        procedure UpdateFormat; virtual;
      public 
        property DateFormat: string[10] read FDateFormat write FDateFormat;

      Then you could do your own control like:

      TMyDateEdit = class(TsDateEdit)
      protected
        procedure UpdateFormat; override;
      ...
      
      procedure TMyDateEdit.UpdateFormat;
      begin
        DateFormat := ...
      end;
      #71082
      Lasse
      Participant

        FormatSettings is a global variable. So, you can also set FormatSettings.ShortDateFormat. GetFormatSettings resets all date and number format variables to their initial values.

        • This reply was modified 3 years, 2 months ago by Lasse.
        • This reply was modified 3 years, 2 months ago by Lasse.
        #71088
        ss141267
        Participant

          Thank you for your information.

          I was thinking if there is anyway I can set the date format on design time, just like TDateTimePicker.Format

          #71090
          Lasse
          Participant

            You can, if you inherit that control and add published Format property. Then just override UpdateFormat and call it when Format property is set.

          Viewing 4 posts - 1 through 4 (of 4 total)
          • You must be logged in to reply to this topic.