Date calculate

I want to make a function to calculate the date.
For example I want to know what is the date for:
1/1/98 + 32, It must be 2/2/98!
And it could use with 2000 year.
Source required! Thank you!!!
Who is Participating?
Roadrunner100598Connect With a Mentor Commented:
Hell chengjjan,

try using TDateTime variables...

function AddToDateStr(DateStr:string; AddDays : integer):TDateTime;
  result:=StrToDateTime(DateStr) + AddDays;

All you have to take care of is that the string has the correct format for your local installation - as I'm from Germany my delphi will accept:

  ADateTimeVar:=AddToDateStr('01.01.1998', 32);

If the result should be a string

  FormatDateTime('DD.MM.YY', ADateTimeVar) will do the job.

Maybe you would like too lookup

FormatDateTime, TDateTime, StrToDateTime, StrToDate, StrToTime, Now, Date, Format, FormatFloat

in the delphi help. They are very nice to use and will help you with most data conversion problems.

I hope that's what you where looking for...

This question is nearly meaningless because you can perform date arithmetic directly on a TDateTime. If you bothered to read the manuals you would be aware that TDateTime is an alias for Double which is an IEEE8 floating point number in which units are days (and therefore .5 = 12 hours and .25 = 6 hours).

You are committing the classic error of confusing a string representation of a value with the value itself. Never operate on the picture string because (a) the formatting is locale dependent and (b) if at any point a user has the opportunity to describe the year, it is possible that the user will use two digits, introducing the y2k problem.
just an example to illustrate wamoz comment:

two add x days to date y, do the following:

result := trunc(y) + x; // result and y: TDateTime, x: Integer;

ok, this doesn't look very nice. look at this:

assuming Edit1 containing a date
         Edit2 containing the number of days to add
         Edit3 shall represent the result of date + days to add

you could do as follows:

Edit3.Text := DateTimeToStr(Trunc(StrToDateTime(Edit1.Text)) + StrToInt(Edit2.Text));


Black Death.
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.


worx with 2000.

And of course with 29th of febuary 2000 as well...
chengjianAuthor Commented:
Sorry for my poor English!
I means could I use such statement:
date1 + date2 = date3?
Date3 - Date2 = Date1?
or something else!
Thanks a lot!
No. You obviously haven't read the manual yet. I quote the Delphi online help for TDateTime, which you obviously haven't bothered to read:

Delphi stores date and time values in the TDateTime type. The integral part of a TDateTime value is the number of days that have passed since 12/30/1899. The fractional part of a TDateTime value is the time of day.


Date1 := Date; //today
Date2 := Date - 1; //yesterday
Date3 := Date + 1; //tomorrow
Date4 := Date + 7; //one week in the future
Date5 := Date1 + Date2; //about 98 years in the future

I suggest you RTFM.
chengjianAuthor Commented:
Thank you a lot, But now my program must do such things:
I want to know how many days between today and another day!
How can i do this?
Or is there a function someone has already written?
  d1, d2: TDateTime;
  diff  : Integer;
  d1 := now - 1;
  d2 := now;
  diff := trunc(d2 - d1);

-> will result in diff=1 (one day between yesterday and today)

-> if using in your program, watch that you subtract the minor value from the major one


Black Death.
Oh for God's sake have you considered thinking yet, or are you trying to be cute? We already told you that a date is stored as a fractional count of days from a reference date.

Surely even a child can see that if you represent two dates in this manner, then subtracting one from the other will give you the number of days between. In fact the manuals actually point this out for you. Perhaps you don't have any manuals. Gee, I wonder why...

I will not waste any more time on this.
TDateTime stores the number of days since a given base date (30.12.1899). Therefore today is day 36138. Tomorrow will be 36139. The fraction of TDateTime give you the time (as wamoz already told you) 12:00 = 0.5, 18:00 = 0.75 etc. One second is 1 / (24*60*60) - there is even a constant SecsPerDay and MSecsPerDay. So today, noon, will be 36138.5 as TDateTime.

Now you can apply this knowledge...

  D, D1 : TDateTime;
  D1:=D+1;{ tomorrow }
  D1:=D-7; { Last week }
and so on... (look into wamoz statement above!)

Or... you have two edit fields... Edit1.Text and Edit2.Text hold valid dates...

  D1, D2 : TDateTime;
  Difference : double;
  DifferenceHoures : double;
    D1:=StrToDateTime(Edit1.Text);  { converts to TDateTime }
    D2:=StrToDateTime(Edit2.Text);  { converts to TDateTime }
    // Wrong dates!

To get the difference in days between D1 and D2 just do
  Difference:=trunc(D2-D1); { Number of days }
or how about the difference in hours?

.or how about timing a routine...

  T : TDateTime;
  T:=Now;  { current time }
  ... some code you want to measure that needs more than just a few milliseconds...
  Label1.Caption:=FormatDateTime('HH:NN:SS', T);

Also you can use TDateTime like every Double:

  Label1.Caption:=Format('Today in seconds since 31.12.1899:  %.2f', [Date*SecsPerDay]);

But anyway..

PLEASE READ THE ONLINE DOCUMENTATION. It's all in there - with explanations and examples. I already gave you the topics in my answere above:

TDateTime, StrToDateTime, Format, FormatDateTime, Now, Date,...

chengjianAuthor Commented:
I am so sorry for this!
These days I cannot use my delphi at home because my computer has some problem. I reach there in my company. I am thingking this problem for a program I will make.
I will end this quession.
Sorry again!
hey wamoz!

does your last comment by any chance refer to mine?
or to chengjian's?
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.

All Courses

From novice to tech pro — start learning today.