 # Thread: Age if years, months and days

1. ## Age if years, months and days

While answering a question on the board I wrote a xbasic code to calculate the difference in two dates to display in years, months and days.
I am presenting that code here for future use by any one interested in that.
These codes are done in
1. Xdialog.
2. Function
3. Web dialog.
Here are the code segments.

Xdilog method:
Code:
```'Date Created: 17-Jan-2014 05:05:09 AM
'Last Updated: 18-Jan-2014 09:30:18 AM
'Created By  : Govindan
'Updated By  : Govindan
'Create an XDialog dialog box to prompt for parameters.
DIM SHARED startDate as D
DIM SHARED endDate as D
DIM SHARED varC_result as C
ok_button_label = "&OK"
cancel_button_label = "&Cancel"
varC_result = ui_dlg_box("Date Difference ",<<%dlg%
{region}
{endregion};
{region}
Start Date:| [%DATE;P=popup.calendar(dtoc(startDate));I=popup.calendar%.40startDate];
End Date:| [%DATE;P=popup.calendar(dtoc(endDate));I=popup.calendar%.40endDate];
{endregion};
{line=1,0};
{region}
<*15=ok_button_label!OK> <15=cancel_button_label!CANCEL>
{endregion};
%dlg%,<<%code%
if startDate > endDate then
msgbox("Difference","Error start date is higher than the end date")
end
end if

dim y1 as n
dim y2 as n
dim m1 as n
dim m2 as n
dim d1 as n
dim d2 as n
dim dY as n
dim dM as n
dim dD as n
dim leapYr as l

y1 = year(startDate)
y2 = year(endDate)
m1 = month(startDate)
m2 = month(endDate)
d1 = day(startDate)
d2 = day(endDate)

select
case mod(y2,4)>0
leapYr = .f.
case mod(y2,400)=0
leapYr = .t.
case mod(y2,100)=0
leapYr = .f.
case else
leapYr = .t.
end select

if ( d2-d1 < 1)
select
case m2 = 1
d2 = d2+31
case m2 = 2
if leapYr
d2 = d2+29
else
d2 = d2+28
end if
case m2 = 3
d2 = d2+31
case m2 = 4
d2 = d2+30
case m2 = 5
d2 = d2+31
case m2 = 6
d2 = d2+30
case m2 = 7
d2 = d2+31
case m2 = 8
d2 = d2+31
case m2 = 9
d2 = d2+30
case m2 = 10
d2 = d2+31
case m2 = 11
d2 = d2+30
case m1 = 12
d2 = d2+31
end select
m2 = m2-1
end if
if (m2 < m1)
y2 = y2-1
m2 = m2+12
end if
dY = y2-y1
dM = m2-m1
dD = d2-d1
msgbox("Difference","the difference is "+dy+" year(s) " +dM+" month(s) "+dD+" day(s) ")

%code%)```

Function method:
Code:
```Date Created: 17-Jan-2014 06:39:07 AM
'Last Updated: 18-Jan-2014 09:36:31 AM
'Created By  : Govindan
'Updated By  : Govindan
FUNCTION age_full AS C (date_of_birth as d, endDate as d )

if date_of_birth > endDate then
age_full = "Date of birth is later than the end date!"
else
dim y1 as n
dim y2 as n
dim m1 as n
dim m2 as n
dim d1 as n
dim d2 as n
dim dY as n
dim dM as n
dim dD as n
dim leapYr as l

y1 = year(date_of_birth)
y2 = year(endDate)
m1 = month(date_of_birth)
m2 = month(endDate)
d1 = day(date_of_birth)
d2 = day(endDate)

select
case mod(y2,4)>0
leapYr = .f.
case mod(y2,400)=0
leapYr = .t.
case mod(y2,100)=0
leapYr = .f.
case else
leapYr = .t.
end select

if ( d2-d1 < 1)
select
case m2 = 1
d2 = d2+31
case m2 = 2
if leapYr
d2 = d2+29
else
d2 = d2+28
end if
case m2 = 3
d2 = d2+31
case m2 = 4
d2 = d2+30
case m2 = 5
d2 = d2+31
case m2 = 6
d2 = d2+30
case m2 = 7
d2 = d2+31
case m2 = 8
d2 = d2+31
case m2 = 9
d2 = d2+30
case m2 = 10
d2 = d2+31
case m2 = 11
d2 = d2+30
case m1 = 12
d2 = d2+31
end select
m2 = m2-1
end if
if (m2 < m1)
y2 = y2-1
m2 = m2+12
end if
dY = y2-y1
dM = m2-m1
dD = d2-d1
age_full = "Age is "+dy+" years " +dM+" months "+dD+" days "
endi if
END FUNCTION```
Web Dialog method:

Web dialog will have two date fields to fill in
and on submit this code will run.

Code:
```function ageFull as c (e as p)
'debug(1)
dim startDate as d
dim endDate as d
startDate = e.dataSubmitted.start_date
endDate = e.dataSubmitted.end_date
if startDate > endDate
ageFull = "alert('The start date is later than the end date!');"
else

dim y1 as n
dim y2 as n
dim m1 as n
dim m2 as n
dim d1 as n
dim d2 as n
dim dY as n
dim dM as n
dim dD as n
dim leapYr as l

y1 = year(startDate)
y2 = year(endDate)
m1 = month(startDate)
m2 = month(endDate)
d1 = day(startDate)
d2 = day(endDate)

select
case mod(y2,4)>0
leapYr = .f.
case mod(y2,400)=0
leapYr = .t.
case mod(y2,100)=0
leapYr = .f.
case else
leapYr = .t.
end select

if ( d2-d1 < 1)
select
case m2 = 1
d2 = d2+31
case m2 = 2
if leapYr
d2 = d2+29
else
d2 = d2+28
end if
case m2 = 3
d2 = d2+31
case m2 = 4
d2 = d2+30
case m2 = 5
d2 = d2+31
case m2 = 6
d2 = d2+30
case m2 = 7
d2 = d2+31
case m2 = 8
d2 = d2+31
case m2 = 9
d2 = d2+30
case m2 = 10
d2 = d2+31
case m2 = 11
d2 = d2+30
case m1 = 12
d2 = d2+31
end select
m2 = m2-1
end if
if (m2 < m1)
y2 = y2-1
m2 = m2+12
end if
dY = y2-y1
dM = m2-m1
dD = d2-d1
ageFull = "alert('The difference is "+dy+" Years "+dM+" Months "+dD+" Day(s) "+"');"

end if```  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•