Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is (2/28/2003 - 2/01/2003) posible ? 1

Status
Not open for further replies.

yomyom

Programmer
Dec 23, 2002
119
GB
Hello all...
How do I compare dates and return actual values i.e
Yesterday - today should equal 1.
Are there routines for doing this ?
(2/28/2003 - 2/1/2003) + 1 ?
Please help....
I'm actually trying to calculate tax based on the number of start and end days picked from picker1 and picker2 components.
If only a month was picked for example, then tax should be
((date2 - date1)+1 / 365)* taxrate ==> to give actual tax for a given period; where (date2 - date1) would evaluate to 30 or 31 or 28 or 29 depending on the month. A shorter period should also result in the number of days between both dates.
All help will be appreciated.
yom yom.
 
Where do I find dateutils unit? I use delphi 4!
yomyom.
 
Also another very simple way to subtract dates if you happen to have a much earlier version of Delphi

var
Date1, Date2 : TDateTime;
DaysBetween : Double;

begin

Date1:=StrToDate('02/25/2003');
Date2:=StrToDate('02/26/2003');

DaysBetween:=Date2-Date1+1;

ShowMessage(FloatToStr(DaysBetween));

end;
 
You might also want to take a look at the "esbdates" unit.
It has a bunch of useful time/date functions.
I am not sure where the best place is to get it from,
but it should be available all over the net, just do a

 
hi !

var
d1, d2 : Tdatetime;
dresult : variant;

begin
d1 := strtodate('02/24/2003');
d2 := strtodate('02/26/2003');
dresult := (d2 - d1) + 1;
end;
 
Give this a shot, it is a function that gives the number of days between the first of the year and the current day (ie Feb 10th gives 41)...

//passed date is 'Now'
function FindDayOfYear(dDate: TDateTime): string;
var
Year, Month, Day: Word;
dTotal, dRounded: double;
begin
DecodeDate(dDate, Year, Month, Day);
dRounded := Round(dDate);
if dRounded < dDate then
dRounded := dRounded + 1;
dTotal := dRounded - EncodeDate(Year,1,1);
Result := FloatToStr(dTotal) + ' ' + IntToStr(Year);
end;
 
Thanks for your reponses.
I settled for :=
.....
var date1,date2: TDate;
DaysBetween: double;
begin
date1 := dateTimePicker1.date;
date2 := dateTimePicker2.date;
DaysBetween := date2 - date1 + 1;
......
Taxpaid := Totb4tax * (daysbetween/365) * taxrate;

hope this will also help others....
yomyom.
 
[tt]
if ( year mod 4 = 0 ) then DaysInYear:= 366
else DaysInYear:=365;
[/tt]

A little bit of time trivia:
How many days were in the month of September, 1752 ?
 
Delphi has an IsLeapYear function.
Brian
&quot;There are 2 kinds of people in the world, those that divide people into two groups and those that don't. I belong to the second group.&quot; - tag line I stole
 
Thanks for your further reponses.
If is leap year works then the code would be:=
.....
var date1,date2: TDate;
DaysBetween: double;
DivBy: integer;
begin
date1 := dateTimePicker1.date;
date2 := dateTimePicker2.date;
DaysBetween := date2 - date1 + 1;
......
if IsLeapyear then
DivBy := 366
else
DivBy := 365;
......
Taxpaid := Totb4tax * (daysbetween/DivBy) * taxrate;

hope this will also help others....
yomyom.
 
yes, that should do it.

BTW, I found that &quot;esbdates&quot; thing, it is at:

It has 275 different functions for working with dates and times,
and is D4 compatible.

Probably more than you need for this program, but it might be handy to stick in your toolbox for the next time you run into something like this...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top