Topic: Increment date years

Hello,

I found here on the forum this script

procedure frmFornitore_dtData_OnChange (Sender: TObject);
begin
  frmFornitore.dtScadenza.date := frmFornitore.dtData.date + 30;
end;

I understand what it does, but my question is how can i change it to increment in years not in days? I know that i can do it by write 365 instead of 30, but what if i want a 10 years increment?

Thanks

Re: Increment date years

Montenegr0 wrote:

Hello,

I found here on the forum this script

procedure frmFornitore_dtData_OnChange (Sender: TObject);
begin
  frmFornitore.dtScadenza.date := frmFornitore.dtData.date + 30;
end;

I understand what it does, but my question is how can i change it to increment in years not in days? I know that i can do it by write 365 instead of 30, but what if i want a 10 years increment?

Thanks

Hello,

If I understand it correctly, by using the same logic you explained I'd asume that it would be 3650

procedure frmFornitore_dtData_OnChange (Sender: TObject);
begin
  frmFornitore.dtScadenza.date := frmFornitore.dtData.date + 3650;
end;

10 years * 365 days = 3,650 days

Re: Increment date years

You can try decoding and encoding the date

procedure frmFornitore_dtData_OnChange (Sender: TObject);
var
  mm,dd,yy:Integer
begin
  DecodeDate(frmFornitore.dtData.date,yy,mm,dd);
  frmFornitore.dtScadenza.date :=  EncodeDate(yy+10,mm,dd);
end;
brian

Re: Increment date years

You could also use the IncMonth function which adds months to the date as follows. 120 equates to 10 years.

Form1.DateTimePicker2.DateTime := IncMonth(Form1.DateTimePicker1.DateTime, 120);

Re: Increment date years

ehwagner wrote:

You could also use the IncMonth function which adds months to the date as follows. 120 equates to 10 years.

Form1.DateTimePicker2.DateTime := IncMonth(Form1.DateTimePicker1.DateTime, 120);

Woa, didn't know this exists, Use this instead of my suggestion. Another knowledge to treasure.

brian

Re: Increment date years

Thank you so much guys

Re: Increment date years

Just another question if someone can help me,

Can anyone help me with a code to do this:

I have a form with two dates and 1 combobox, in this combobox i have already 3 options ( 1year, 5years,10years).

What i need to do is select a date in datetimepicker1 and then choose one of this three options from combobox and then the datetimepicker2 will show the new date with the increment of the years that we choose based on datetimepicker1.

8 (edited by derek 2021-02-25 21:00:29)

Re: Increment date years

Hi,
You could try it like this (see attached).
Derek.

Post's attachments

Attachment icon comboboxyears.zip 339.63 kb, 304 downloads since 2021-02-25 

Re: Increment date years

derek wrote:

Hi,
You could try it like this (see attached).
Derek.


Hi Derek,
I'm still using the good and old 1.44 free version big_smile

I'm a beginner, i stop working with this for a while but now I'm trying to understand and learn again.

Can you post the code here please?

10 (edited by derek 2021-02-26 11:47:33)

Re: Increment date years

Hi,
If you are still using the free version, then the function 'incmonth' is not supported (MVD has come such a long way since 1.44!).
In that case, you need to use something like 'decode' and 'encode' as Brian suggested in his earlier post.
See attached for an example.
Derek.

Post's attachments

Attachment icon 1.zip 334.64 kb, 295 downloads since 2021-02-26 

Re: Increment date years

You can also try creating your own version of IncMonth if it is not available in ver1.44,

function AddMonth(const DateTime:TDateTime; NumberOfMonths: Integer): TDateTime;
var
    _m,_d,_y : Integer;
begin
    DecodeDate(DateTime,_y,_m,_d);
    result := EncodeDate(_y,_m+NumberOfMonths,_d);
end;

Add it in your code and you can use AddMonth just like of the IncMonth

brian

Re: Increment date years

thank you so much guys