Solved

Date calculate

Posted on 1998-12-06
13
427 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
  • 4
  • 3
  • 3
  • +1
13 Comments
 
LVL 1

Accepted Solution

by:
Roadrunner100598 earned 30 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

by:BlackDeath
Comment Utility
btw:

worx with 2000.

BD
0
 
LVL 1

Expert Comment

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

Author Comment

by:chengjian
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Expert Comment

by:wamoz
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
hey wamoz!

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

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now