Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# Manipulating TDateTime

Posted on 2001-06-29
Medium Priority
447 Views
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
Question by:Tom Knowlton
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 4
• 3
• 2

LVL 6

Expert Comment

ID: 6238702
var
dt : TDateTime;

add 1 day: dt := dt + 1;

GL
Mike
0

LVL 6

Expert Comment

ID: 6238725
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

LVL 13

Accepted Solution

Epsylon earned 200 total points
ID: 6238737
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

LVL 5

Author Comment

ID: 6238761
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

LVL 5

Author Comment

ID: 6238767
Looking back, I guess adding days to a TDateTime was kind of a stupid question...ha ha.
0

LVL 5

Author Comment

ID: 6238773
Thank you both!
0

LVL 13

Expert Comment

ID: 6238779
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

LVL 13

Expert Comment

ID: 6238782
Thank you for the points  :o)
0

LVL 5

Author Comment

ID: 6238870
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

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
###### Suggested Courses
Course of the Month9 days, 6 hours left to enroll