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

There is weeks difference using this code to calculate age?? How to so

Status
Not open for further replies.

compu66

Programmer
Dec 19, 2007
71
US
Hi all,

I am unable to figure out the problem.How there is weeks difference coming up.


If sDOB <> "" And sScheduleDate <> "" Then
If IsDate(sDOB) And IsDate(sScheduleDate) Then
Try
sDOB = Convert.ToDateTime(sDOB)
sScheduleDate = Convert.ToDateTime(sScheduleDate)

'CALCULATE NUMBER OF YEARS
sMonths = DateDiff(DateInterval.Month, sDOB, sScheduleDate)

'Calculate the Months
'sMonths = (DateDiff("m", sDOB, sScheduleDate))
If (DatePart("d", sDOB) > DatePart("d", sScheduleDate)) Then
sMonths = sMonths - 1

End If
If sMonths < 0 Then
sMonths = sMonths + 1
End If

sYears = Math.Floor(sMonths / 12)
sMonths = CInt(sMonths Mod 12)

Return CStr(sYears) & " years " & CStr(sMonths) & " Months"

End Function

Thanks for any help in advance.
 
If you use the Subtract method of a date, you can return a TimeSpan object which has all of these properties built in e.g.
Code:
        Dim d1 As New DateTime
        Dim d2 As New DateTime
        Dim t As New TimeSpan
        t = d2.Subtract(d1)
Then check out whatever properties of the "t" variable are appropriate.


-------------------------------------------------------

Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]
[URL unfurl="true"]http://mdssolutions.co.uk[/url] - Delivering professional ASP.NET solutions
[URL unfurl="true"]http://weblogs.asp.net/marksmith[/url]
 
for example the age of one person is 63yrs and 7.5 months and my code is showing 63yrs and 7 months its off by feww weeks.I think any mathimatical operator that is used might be rounding 7.5 to 7 and showing.


 
so I want to show the exact dob including that .5 months.Is it possible.

Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top