Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 498
  • Last Modified:

Calculate current age in years and months in ASP

I received help for getting the current age in years and months for .NET but now I need it for classic ASP.  I can't figure out how to convert this code to classic ASP.

  Dim dt1, dt2 As Date
        dt1 = DOB
        dt2 = now
        Dim Months As Long
        Dim Diff As Long = DateDiff(DateInterval.Month, dt1, dt2)
        Dim Years As Long = Diff \ 12
        Months = Diff - Years * 12
        formatAge = Years & " y  " & Months & " m "

Open in new window

0
huerita37
Asked:
huerita37
1 Solution
 
Kent DyerCommented:
Hmm  something does not look right..

1 / 2 should be .5..

You need to change:
Dim Years As Long = Diff \ 12

to be:
Dim Years As Long = Diff / 12

HTH,

Kent
0
 
Om PrakashCommented:
Simply replacing "/" is not going to work.
Since this is classic ASP, variables will not have data type. Here is the updated script:

<%
Dim dt1, dt2
dt1 = "17/4/2005"
dt2 = Date()
Dim Months
Dim Diff
Dif = DateDiff("m", dt1, dt2)
Dim Years
Years = cint(Dif / 12)

Months = Dif - Years * 12
formatAge = Years & " y  " & Months & " m "
response.write formatAge
%>

Open in new window

0
 
Scott Fell, EE MVEDeveloperCommented:
This is an easy way to use the same code multiple times in your page or for loops.

function formatAge(x)
formatAge="No date given"
if isdate(x) then
     Months=DateDiff("m", x, date)
     Years=    int(Months / 12)
     Months=  Months - Years * 12
     formatAge= Years &" Y "& Months & " M"
end if
end function

' now play
response.write formatAge("1/18/1998") 
response.write formatAge("xyz") ' will output "No date given"
y=1998
for x = 1 - 12
    response.write formatAge("5/15/"&x+y)&"<br>"
next

Open in new window

0
 
huerita37Author Commented:
Sorry for the delay.  I haven't had a chance to test to make sure I get the correct answer in my program but your code works.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now