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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple date arithmetic 1

Status
Not open for further replies.

teamakesmefart

Technical User
Dec 3, 2003
10
0
0
GB
Hi All

How can I perform a simple addition on a Date variable in ObjectPAL? Surely procedure for such are built into Paradox?

What I want to do is add 1 year to a specified date. In a variation I may also want to find (1 year - 1 day) + specified date.

Obviously this would need to take into account whether a leap year is included in the calculable period.


Many thanks
Pete
Devon, UK
 
For all things date related, see Rick Kelly's site.

I believe he has a calendar library, just not sure if there is a free version.


For the basic 'add a year', you could do something like:

newdate=date(
string(day(today()))+
"/"+
string(month(today()))+
"/"+
string(year(today())+1)
)

for the other, something like:

newdate-1

But what would you be meaning about finding a date-1, and then adding a specified date?

Tony McGuire
"It's not about having enough time. It's about priorities
 
Transposed day/month in first post. MAN do I wish this forum had an edit function!

As to leapyear, I think you would just test for the current year being a leapyear and apply a day-1 to the newDate calculation if day(today())=29 and month(today())=2 :

dt=datetime()
if dt.isleapyear() then
if (month(dt)=2 and day(dt)=29) then
dy=28
endif
else
dy=day(dt)
endif

newdate=date(
string(month(dt)),+
"/"+
string(dy)+
"/"+
string(year(dt)+1)
)

If you need detailed calendaring/date calculations, get Rick's stuff.

Tony McGuire
"It's not about having enough time. It's about priorities
 
Sometimes you can bypass the need to use ObjectPal simply by placing DATE calculations directly on Form and Report fields. I routinely use the following three items:

1. Calculating a person’s AGE as of today’s date. (Uses “Date of Birth” from Clients.DB).

iif([Clients.Date of Birth]>"",LongInt(number(Today()-[Clients.Date of Birth])/365.25),"")

Note: If you need to calculate the person’s age up through a specific date other than today, substitute “Today()” in the formula with: (A) a table date field name, or (B) a literal date. Example: “Date(“03/01/2005”)”


2. Calculating an employee’s years of service.

iif([History.Date Terminated]>"",round(number([History.Date Terminated] - [History.Date Hired])/365.25,1),iif([History.Date Hired] > 0,round(number(Today() - [History.Date Hired])/365.25,1),""))

Note: Partial years are converted and expressed as tenths (5.25 rounds to 5.3) as opposed to months (5 years and 3 months).


3. Sometimes I need to calculate years of Service as of a specific Cutoff Date given to me by management. In this case I use SQL to generate a Report table, and add text such as the following to the Select statement:

SELECT D.SSN, D.“Date Hired”, “12/31/2004" as CutoffDate, ...

My output table thus contains an extra Date Field column called CutoffDate. Each table row contains the constant date of 12/31/2004 in this column. I can then use:

CutoffDate - Date Hired

in report calculations.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top