Solved

# Date calculate

Posted on 1998-12-06
Medium Priority
435 Views
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!!!
0
Question by:chengjian
[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
• 3
• +1

LVL 1

Accepted Solution

ID: 1349501
Hell chengjjan,

try using TDateTime variables...

begin
end;

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:

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...

0

LVL 1

Expert Comment

ID: 1349502
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.
0

LVL 1

Expert Comment

ID: 1349503
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));

rex,

Black Death.
0

LVL 1

Expert Comment

ID: 1349504
btw:

worx with 2000.

BD
0

LVL 1

Expert Comment

ID: 1349505
And of course with 29th of febuary 2000 as well...
0

LVL 1

Author Comment

ID: 1349506
Sorry for my poor English!
I means could I use such statement:
date1 + date2 = date3?
or
Date3 - Date2 = Date1?
or something else!
Thanks a lot!
0

LVL 1

Expert Comment

ID: 1349507
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.

Thus

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.
0

LVL 1

Author Comment

ID: 1349508
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?
0

LVL 1

Expert Comment

ID: 1349509
var
d1, d2: TDateTime;
diff  : Integer;
begin
d1 := now - 1;
d2 := now;
diff := trunc(d2 - d1);
end;

-> 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.
0

LVL 1

Expert Comment

ID: 1349510
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.
0

LVL 1

Expert Comment

ID: 1349511
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...

var
D, D1 : TDateTime;
begin
D:=Date;
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...

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

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?
DifferenceHoures:=(D1-D1)*24;

.or how about timing a routine...

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

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,...

0

LVL 1

Author Comment

ID: 1349512
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!
0

LVL 1

Expert Comment

ID: 1349513
hey wamoz!

does your last comment by any chance refer to mine?
or to chengjian's?
0

## Featured Post

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the inâ€¦
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 this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on tâ€¦
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/weâ€¦
###### Suggested Courses
Course of the Month8 days, 14 hours left to enroll