Solved

Manipulating TDateTime

Posted on 2001-06-29
9
441 Views
Last Modified: 2010-04-06
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
Comment
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
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 6

Expert Comment

by:edey
ID: 6238702
var
 dt : TDateTime;

add 1 day: dt := dt + 1;


GL
Mike
0
 
LVL 6

Expert Comment

by:edey
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

by:
Epsylon earned 50 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Author Comment

by:Tom Knowlton
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

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

Author Comment

by:Tom Knowlton
ID: 6238773
Thank you both!
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 6238779
You can quickly test those functions with a TMonthCalendar and this code:

procedure TForm1.Button1Click(Sender: TObject);
begin
  MonthCalendar1.Date := AddDaysToTDate(1, MonthCalendar1.Date);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  MonthCalendar1.Date := AddMonthsToTDate(1, MonthCalendar1.Date);
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  MonthCalendar1.Date := AddYearsToTDate(1, MonthCalendar1.Date);
end;
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 6238782
Thank you for the points  :o)
0
 
LVL 5

Author Comment

by:Tom Knowlton
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

731 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question