Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

Calculate current age in years and months in ASP

Posted on 2013-05-28
Medium Priority
472 Views
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 "
``````
0
Question by:huerita37
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• Learn & ask questions

LVL 17

Expert Comment

ID: 39203573
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

LVL 22

Accepted Solution

Om Prakash earned 2000 total points
ID: 39203594
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
%>
``````
0

LVL 54

Expert Comment

ID: 39203697
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
``````
0

Author Closing Comment

ID: 39213595
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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses
Course of the Month10 days, 8 hours left to enroll

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.