• Status: Solved
• Priority: Medium
• Security: Public
• Views: 450

Manipulating TDateTime

Does anyone know of an easy way to add a certain number of days, months, or years to a TDateTime variable?

As in:

function AddDaysToTDate(temp_days : integer; temp_TDateTime : TDateTime) : TDateTime;

function AddMonthsToTDate(temp_months : integer; temp_TDateTime : TDateTime) : TDateTime;

function AddYearsToTDate(temp_years : integer; temp_TDateTime : TDateTime) : TDateTime;

You pass in the amount you want to increment by (days, months, or years) and the TDateTime you are modifying.  Function spits back the TDateTime value that reflects the increment.

Thank you,

Tom

0
Tom Knowlton
• 4
• 3
• 2
1 Solution

Commented:
var
dt : TDateTime;

add 1 day: dt := dt + 1;

GL
Mike
0

Commented:
var
dt : TDateTime;
y,m,d : word;
begin
dt := now;//today.currentTime;
dt := dt + 1/24;//one hour from now;
dt := dt+1;//tomorrow
decodeDate(dt,y,m,d);
dt := encodeDate(y,m+1,d-1);//one month from now ('cause dt _was_ tomorrow)
dt := encodeDate(y+1,m,d-1);//one year from tomorrow;

GL
Mike
0

Commented:
function AddDaysToTDate(temp_days : integer; temp_TDateTime : TDateTime) : TDateTime;
begin
Result := temp_TDateTime + temp_days;
end;

function AddMonthsToTDate(temp_months : integer; temp_TDateTime : TDateTime) : TDateTime;
begin
Result := IncMonth(temp_TDateTime, temp_months);
end;

function AddYearsToTDate(temp_years : integer; temp_TDateTime : TDateTime) : TDateTime;
begin
Result := IncMonth(temp_TDateTime, temp_years * 12);
end;
0

Web developerAuthor Commented:
Ahhh...

While you are correct, Mike, I think I will go with Epsylon on this one.

IncMonth is what I actually needed.

I did not know if such a function existed for TDateTime variables.  I was thinking I would have to calculate the number of days in a Month range:

5 months x approx 30 days per month := 150 days (except not all months have 30 days)

I am guessing IncMonth accounts for this?

Thanks Epsylon.
0

Web developerAuthor Commented:
Looking back, I guess adding days to a TDateTime was kind of a stupid question...ha ha.
0

Web developerAuthor Commented:
Thank you both!
0

Commented:
You can quickly test those functions with a TMonthCalendar and this code:

procedure TForm1.Button1Click(Sender: TObject);
begin
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
end;
0

Commented:
Thank you for the points  :o)
0

Web developerAuthor Commented:
No problem.  I hope Mike is not too upset.  I guess it is really up to me who I give points to, but I still hate to step on toes...know what I mean?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.