Solved

Date calculate

Posted on 1998-12-06
13
431 Views
Last Modified: 2010-04-04
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
Comment
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
  • Learn & ask questions
  • 4
  • 3
  • 3
  • +1
13 Comments
 
LVL 1

Accepted Solution

by:
Roadrunner100598 earned 30 total points
ID: 1349501
Hell chengjjan,

try using TDateTime variables...

function AddToDateStr(DateStr:string; AddDays : integer):TDateTime;
begin
  result:=StrToDateTime(DateStr) + AddDays;
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:

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

Roadrunner
0
 
LVL 1

Expert Comment

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

by:BlackDeath
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
Industry Leaders: 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 1

Expert Comment

by:BlackDeath
ID: 1349504
btw:

worx with 2000.

BD
0
 
LVL 1

Expert Comment

by:Roadrunner100598
ID: 1349505
And of course with 29th of febuary 2000 as well...
0
 
LVL 1

Author Comment

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

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

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

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

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

by:Roadrunner100598
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,...


Roadrunner
0
 
LVL 1

Author Comment

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

by:BlackDeath
ID: 1349513
hey wamoz!

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

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…

734 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