macoxy
17th February 2004, 14:35
I have some date in a table, and i have some string like 30 wich means 30 months. What i would like is to add 30 months to that date
how???

klixy23
17th February 2004, 18:05
long y, m, d
long month_count
string month.str | string field from table or form i.e. "30"
domain tcdate date.1 | date field from table
domain tcdate date.2 | new date

num.to.date(date.1, y, m, d)
month_count = lval(month.str)
for i = 1 to month_count
m = m + 1
if m > 12 then
y = y + 1 | add 1 year
m = 1 | set month to January
endif
endfor
| convert to date
date.2 = date.to.num(y, m, d)



For less precision add 30 * month_count to the date.

zardoz
17th February 2004, 19:11
Alternative:

long y, m, d
long month_count
string month.str | string field from table or form i.e. "30"
domain tcdate date.1 | date field from table
domain tcdate date.2 | new date

num.to.date(date.1, y, m, d)
month_count = lval(month.str)
date2 = date.to.num( (y + month_count / 12),
(m + month_count) \ 12 + 1,
d)

Hitesh Shah
18th February 2004, 08:02
Also check for non existing date like 29,30,31 also . When u try to use date.to.num on non-existing dates , it returns 0 .

Zardos, it seems ur code may lead to wrong year number and month . Just check.

klixy23
18th February 2004, 09:01
OK. Let's tune the code.

| ... changed code
| convert to date
date.2 = 0
while not date.2
date.2 = date.to.num(y, m, d)
d = d + 1
| or d = d - 1 to adjust d to an valid date
endwhile