Recent

Author Topic: TDateEdit - And dates before 1976  (Read 1059 times)

OC DelGuy

  • Full Member
  • ***
  • Posts: 195
TDateEdit - And dates before 1976
« on: March 10, 2025, 08:27:59 pm »
When I use a TDateEdit and select a date before 1976, the dates show in the 20xx's.   Example: 1975 shows up as 2075
I tried searching in Google, but all I can get is changing the format.  And I know that's not going to affect the problem above.
I don't know what to search for , because the The TDateEdit already returns a 4 digit year.
How can I select dates before 1976?

I've attached the output of the application
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, EditBtn, StdCtrls, DateUtils;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     DateEdit1: TDateEdit;
  16.     Memo1: TMemo;
  17.     procedure DateEdit1Change(Sender: TObject);
  18.   private
  19.  
  20.   public
  21.  
  22.   end;
  23.  
  24. var
  25.   Form1: TForm1;
  26.  
  27. implementation
  28.  
  29. {$R *.lfm}
  30.  
  31. { TForm1 }
  32.  
  33. procedure TForm1.DateEdit1Change(Sender: TObject);
  34. begin
  35.   Memo1.Clear;
  36.   Memo1.Lines.Add(DateToStr(DateEdit1.Date));
  37.   Memo1.Lines.Add(IntToStr(YearOf(DateEdit1.Date)));
  38. end;
  39.  
  40. end.
  41.  
I looked throu the properties
Free Pascal Lazarus Version #: 2.2.4
Date: 24 SEP 2022
FPC Version: 3.2.2
Revision: Lazarus_2_2_4
x86_64-win64-win32/win64

cdbc

  • Hero Member
  • *****
  • Posts: 2064
    • http://www.cdbc.dk
Re: TDateEdit - And dates before 1976
« Reply #1 on: March 10, 2025, 08:54:35 pm »
Hi
Look for some property that rhymes with 'TwoDigitYearWindow', 'TwoDigitYearAxis' or something in the sense.
It comes down to how it interprets the 2 digit year, basically it sets up a window of -50..+50 years from the current year, meaning that '75' -> 2075 and '76' -> 1976  ...you dig?!?
If it was cleverly developed, I'd expect a property, with which you can adjust said /window/...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

wp

  • Hero Member
  • *****
  • Posts: 12753
Re: TDateEdit - And dates before 1976
« Reply #2 on: March 10, 2025, 09:11:46 pm »
It's a element of the (Default)FormatSettings record (https://docwiki.embarcadero.com/Libraries/Sydney/en/System.SysUtils.TFormatSettings.TwoDigitYearCenturyWindow):
Code: Pascal  [Select][+][-]
  1. var
  2.   DefaultFormatSettings : TFormatSettings = (
  3.     ...
  4.     ShortDateFormat: 'd/m/y';
  5.     LongDateFormat: 'dd" "mmmm" "yyyy';
  6.     TimeAMString: 'AM';
  7.     TimePMString: 'PM';
  8.     ShortTimeFormat: 'hh:nn';
  9.     LongTimeFormat: 'hh:nn:ss';
  10.     ShortMonthNames: ('Jan','Feb','Mar','Apr','May','Jun',
  11.                       'Jul','Aug','Sep','Oct','Nov','Dec');
  12.     LongMonthNames: ('January','February','March','April','May','June',
  13.                      'July','August','September','October','November','December');
  14.     ShortDayNames: ('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
  15.     LongDayNames:  ('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
  16.     TwoDigitYearCenturyWindow: 50;
  17.   );
  18.  
  19.   FormatSettings : TFormatSettings absolute DefaultFormatSettings;
  20.  

OC DelGuy

  • Full Member
  • ***
  • Posts: 195
Re: TDateEdit - And dates before 1976
« Reply #3 on: March 11, 2025, 04:43:09 am »
Hi
Look for some property that rhymes with 'TwoDigitYearWindow', 'TwoDigitYearAxis' or something in the sense.
It comes down to how it interprets the 2 digit year, basically it sets up a window of -50..+50 years from the current year, meaning that '75' -> 2075 and '76' -> 1976  ...you dig?!?
If it was cleverly developed, I'd expect a property, with which you can adjust said /window/...
Regards Benny
If you're talking about a TDateEdit property that is listed in the Object inspector, then, I did that before I posted and there is no such property.  There is DateFormat and DateOrder.  Nothing that affects the actual inside workings of TDateEdit.  If you're talking about one of those "hidden" properties that don't show up in the Object Inspector, then I haven't seen it.  I've looked at the reference in https://wiki.lazarus.freepascal.org/TDateEdit and https://www.freepascal.org/docs.html (which, curiously, doesn't have a TDateEdit entry).
And, BTW, what I mean by "hidden" is something like the "Date" property.  Date is not listed in the Object Inspector, but TDateEdit.Date does exist.  It's just... "hidden".

It's a element of the (Default)FormatSettings record (https://docwiki.embarcadero.com/Libraries/Sydney/en/System.SysUtils.TFormatSettings.TwoDigitYearCenturyWindow):
Code: Pascal  [Select][+][-]
  1. var
  2.   DefaultFormatSettings : TFormatSettings = (
  3.     ...
  4.     ShortDateFormat: 'd/m/y';
  5.     LongDateFormat: 'dd" "mmmm" "yyyy';
  6.     TimeAMString: 'AM';
  7.     TimePMString: 'PM';
  8.     ShortTimeFormat: 'hh:nn';
  9.     LongTimeFormat: 'hh:nn:ss';
  10.     ShortMonthNames: ('Jan','Feb','Mar','Apr','May','Jun',
  11.                       'Jul','Aug','Sep','Oct','Nov','Dec');
  12.     LongMonthNames: ('January','February','March','April','May','June',
  13.                      'July','August','September','October','November','December');
  14.     ShortDayNames: ('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
  15.     LongDayNames:  ('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
  16.     TwoDigitYearCenturyWindow: 50;
  17.   );
  18.  
  19.   FormatSettings : TFormatSettings absolute DefaultFormatSettings;
  20.  

Two Questions:  How do I get to this setting? And, How about if the date is 1875, or 1775, or even 75 (as in during the first century)?  All seriousness aside, though, I won't be using 75, but I will be using dates as low as the 1200's.  How do I deal with those?
Free Pascal Lazarus Version #: 2.2.4
Date: 24 SEP 2022
FPC Version: 3.2.2
Revision: Lazarus_2_2_4
x86_64-win64-win32/win64

paweld

  • Hero Member
  • *****
  • Posts: 1351
Re: TDateEdit - And dates before 1976
« Reply #4 on: March 11, 2025, 06:41:03 am »
If the short date format has only two digits of the year, the year will be between (YearOf(Now) - DefultFormatSettings.TwoDigitYearCenturyWindow) and (YearOf(Now) + 100 - DefultFormatSettings.TwoDigitYearCenturyWindow).
If you want the range not to be between -50..50, but, for example, -80..20, you can set it as follows:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   DefaultFormatSettings.TwoDigitYearCenturyWindow := 80;
  4. end;
But the best way to solve this problem is to set the format so that the year consists of four digits:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   DateEdit1.DateFormat := 'dd-mm-yyyy';  //dd-day number; mm-month number; yyyy-four digits year,
  4.                                          //more info: https://www.freepascal.org/docs-html/rtl/sysutils/formatchars.html  
  5. end;
Best regards / Pozdrawiam
paweld

OC DelGuy

  • Full Member
  • ***
  • Posts: 195
Re: TDateEdit - And dates before 1976
« Reply #5 on: March 11, 2025, 01:33:36 pm »
But the best way to solve this problem is to set the format so that the year consists of four digits:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   DateEdit1.DateFormat := 'dd-mm-yyyy';  //dd-day number; mm-month number; yyyy-four digits year,
  4.                                          //more info: https://www.freepascal.org/docs-html/rtl/sysutils/formatchars.html  
  5. end;

Freaking A mate!  This is what I've been looking for!  I didn't think a format string would affect the workings of the TDateEdit.  And, to think, it was in the Object inspector the whole time!
Free Pascal Lazarus Version #: 2.2.4
Date: 24 SEP 2022
FPC Version: 3.2.2
Revision: Lazarus_2_2_4
x86_64-win64-win32/win64

Zvoni

  • Hero Member
  • *****
  • Posts: 2961
Re: TDateEdit - And dates before 1976
« Reply #6 on: March 11, 2025, 01:47:27 pm »
Freaking A mate!  This is what I've been looking for!  I didn't think a format string would affect the workings of the TDateEdit.  And, to think, it was in the Object inspector the whole time!
Why wouldn't you think that? Especially with 2-Digit-Year

11-11-18
Is this now the end of World War 1 (Nov. 11th, 1918)
or the day of the 3:1 of Manchester City vs.  Man United (Nov. 11th 2018)?
How is any program going to guess what you mean?
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

TRon

  • Hero Member
  • *****
  • Posts: 4260
Re: TDateEdit - And dates before 1976
« Reply #7 on: March 11, 2025, 05:32:31 pm »
How is any program going to guess what you mean?
Oh that is just the new world order. You just call it fraud at your convenience  ;)
Today is tomorrow's yesterday.

OC DelGuy

  • Full Member
  • ***
  • Posts: 195
Re: TDateEdit - And dates before 1976
« Reply #8 on: March 11, 2025, 06:09:55 pm »
Why wouldn't you think that? Especially with 2-Digit-Year

Because formatting usually just affects the way the program puts something on the screen.  It usually does not affect the way the program uses the values of the variables and functions.  Up until today I never knew that formating could actually affect the value of a variable or function.

So when I use the TDateEdit and pick 1976 using the dropdown window and YearOf shows up as 1976 and then pick 1975 using the same dropdown window and it comes out as 2075, my first thought wasn't "formatting issue".
IOW, My brain was thinking: Postulate: Formatting affects the view, never the value.

Even though two year formatting would show 1975 and 2075 both as 75, internally (I thought) the computer still knows the difference.  It just displays them the same way.  Now I see that even though I input 1975, the format setting actually changes it to 2075.  That's changing the actual value.

That is why I would think that.
Free Pascal Lazarus Version #: 2.2.4
Date: 24 SEP 2022
FPC Version: 3.2.2
Revision: Lazarus_2_2_4
x86_64-win64-win32/win64

wp

  • Hero Member
  • *****
  • Posts: 12753
Re: TDateEdit - And dates before 1976
« Reply #9 on: March 11, 2025, 06:36:34 pm »
Run this little program:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. uses
  3.   SysUtils;
  4. begin
  5.   WriteLn(FormatSettings.ShortDateFormat);
  6.   ReadLn;
  7. end.
The output is locale-dependent. For me (Cental Europe) it is "dd.MM.yyyy". "yyyy" means: 4-digit year. How many "y"s are displayed for you? Just one or two? This would mean: 2-digit year! Never ever use that! You should go the the country settings of your operating system and change the "short date format" to a 4-digit setting.

Bart

  • Hero Member
  • *****
  • Posts: 5550
    • Bart en Mariska's Webstek
Re: TDateEdit - And dates before 1976
« Reply #10 on: March 11, 2025, 06:51:45 pm »
Why wouldn't you think that? Especially with 2-Digit-Year
Because formatting usually just affects the way the program puts something on the screen.  It usually does not affect the way the program uses the values of the variables and functions.  Up until today I never knew that formating could actually affect the value of a variable or function.

The getter for the Date property derives the date from the text in the control.
This is by design.
So, if your text has a 2-digit year, then basically all bets are off.

Also, now it is clear why the dateformat is relevant: 12-09-23 is rather undefined if you don't know the format.

Bart

OC DelGuy

  • Full Member
  • ***
  • Posts: 195
Re: TDateEdit - And dates before 1976
« Reply #11 on: March 11, 2025, 09:43:47 pm »
Run this little program:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. uses
  3.   SysUtils;
  4. begin
  5.   WriteLn(FormatSettings.ShortDateFormat);
  6.   ReadLn;
  7. end.
The output is locale-dependent. For me (Cental Europe) it is "dd.MM.yyyy". "yyyy" means: 4-digit year. How many "y"s are displayed for you? Just one or two? This would mean: 2-digit year! Never ever use that! You should go the the country settings of your operating system and change the "short date format" to a 4-digit setting.
Yes, mine says "yy".
My issue is not with the Formatting (the way something is displayed on the screen), but with Format setting actually changing (as I said above) the input.  And also what I'm about to respond below.

The getter for the Date property derives the date from the text in the control.
This is also  part of what I didn't know.  Even though I input (via the little dropdown window of the TDateEdit 1975, the text gets displayed as 75 and the "getter" reads the text.  I was thinking that I would input 1975, the "getter" would get 1975 and THEN the format would make the text in the control a two digit year.

I know I don't know jack about all this, but if the little dropdown window actually has 4 digit years, then shouldn't the "getter" get the year from the dropdown window, and not the text in the control?  I input 1975.  Regardless of formatting (formatting being "the way it displays on the screen"), the value of DateEdit1.Date should be 1975.

It just occurred to me now that if the FormatSettings.ShortDateFormat is 'yy' then the dropdown window should just show the years 1976 to 2075.


Is there a way to change the FormatSettings.ShortDateFormat to 'yyyy' permanently?
Free Pascal Lazarus Version #: 2.2.4
Date: 24 SEP 2022
FPC Version: 3.2.2
Revision: Lazarus_2_2_4
x86_64-win64-win32/win64

Bart

  • Hero Member
  • *****
  • Posts: 5550
    • Bart en Mariska's Webstek
Re: TDateEdit - And dates before 1976
« Reply #12 on: March 11, 2025, 11:22:00 pm »
I know I don't know jack about all this, but if the little dropdown window actually has 4 digit years, then shouldn't the "getter" get the year from the dropdown window, and not the text in the control?  I input 1975.

It's not that simple.
Users can type into the control, not using the dropdown calendar.
At the moment you read Date, the control does not know wether you typed it or used the calendar to fill it.
Note: if DirectInput is False, it does not parse the text, but it uses the internal field FDate (which is set when using the calendar).

So, if you give the user the ability to type (DirectInput=True, which is the default), you're basically screwed if you use a 2-digit year.
If you insist on having a 2-digit year (which IMHO is a WTF in itself), then set DirectInput to False.

And yes, we could add some overhead to keep track of how the date was entered (in case of DirectInput=True), but it would lead to inconsistencies: if you use the calendar (in your example) it gives the correct date, if you don't it gives you the incorrect date.
I don't think we'll add such logic.

So, you have 3 solutions:
  • Use a 4-digit year (IMO the preferred solution)
  • Set DirectInput=False
  • Use another control and do the logic yourself

Bart

OC DelGuy

  • Full Member
  • ***
  • Posts: 195
Re: TDateEdit - And dates before 1976
« Reply #13 on: March 17, 2025, 09:54:29 am »
If you insist on having a 2-digit year (which IMHO is a WTF in itself), then set DirectInput to False.
Dear God, No Sir!  :o  I do not insist!   :D  I would rather 4 digit all the way.


if you use the calendar (in your example) it gives the correct date
I'm not sure I understand.  I used the calendar and it did come out wrong (for years before 1976).  That's how I got started on this topic.  I ONLY used the calendar and picked 1975.  When I then printed it in the TMemo, It came out as 2075.  I've added  DateEdit1.DateFormat := 'dd-mm-yyyy'; So I don't think I'll be seeing that in the future.  But, I'd sure like to know how to set that format permanently, instead of overriding it every time in each application.


So, you have 3 solutions:
  • Use a 4-digit year (IMO the preferred solution)
  • Set DirectInput=False
  • Use another control and do the logic yourself
And I have 3 Answers:
  • Yes, Done did!  :)
  • Understood. Wilco. 8-)
  • We all know: I's aint dat smart! <Where dat Poindexter emoji?>

Free Pascal Lazarus Version #: 2.2.4
Date: 24 SEP 2022
FPC Version: 3.2.2
Revision: Lazarus_2_2_4
x86_64-win64-win32/win64

 

TinyPortal © 2005-2018