DateDiff rounds?

  • Thread starter Thread starter Brian Henry
  • Start date Start date
B

Brian Henry

I am trying to find someones age within 2 months of their birthday on the
given date for the run of the application

Dim d_runOn As DateTime = #4/4/2005#

Dim d_bday As DateTime = #6/26/1986#

Console.WriteLine(String.Format("Ran on {0}, birthdate {1}",
d_runOn.ToShortDateString, d_bday.ToShortDateString))

Console.WriteLine(String.Format("Years difference: {0}",
DateDiff(DateInterval.Year, d_bday, d_runOn.AddMonths(2),
FirstDayOfWeek.System, FirstWeekOfYear.System)))

Console.WriteLine(String.Format("Age of date: {0}",
Math.Floor((DateDiff(DateInterval.Day, d_bday, d_runOn.AddMonths(2),
FirstDayOfWeek.System, FirstWeekOfYear.System) / 365))))



of course the DateInteral.Year rounds so even though the person has no
turned 19 yet, it says they are 19. I need it to say they are still 18 years
old (the second one with the Math.Floor / 365 one) gives the correct age
(18). How would i prevent rounding of the DateInterval.Year one?! or is it
even possible?
 
Brian Henry said:
I am trying to find someones age within 2 months of their birthday on
the given date for the run of the application

Dim d_runOn As DateTime = #4/4/2005#

Dim d_bday As DateTime = #6/26/1986#

Console.WriteLine(String.Format("Ran on {0}, birthdate {1}",
d_runOn.ToShortDateString, d_bday.ToShortDateString))

Console.WriteLine(String.Format("Years difference: {0}",
DateDiff(DateInterval.Year, d_bday, d_runOn.AddMonths(2),
FirstDayOfWeek.System, FirstWeekOfYear.System)))

Console.WriteLine(String.Format("Age of date: {0}",
Math.Floor((DateDiff(DateInterval.Day, d_bday, d_runOn.AddMonths(2),
FirstDayOfWeek.System, FirstWeekOfYear.System) / 365))))



of course the DateInteral.Year rounds so even though the person has
no turned 19 yet, it says they are 19. I need it to say they are
still 18 years old (the second one with the Math.Floor / 365 one)
gives the correct age (18). How would i prevent rounding of the
DateInterval.Year one?! or is it even possible?

Public Shared Function GetAge( _
ByVal BirthDay As Date, _
ByVal Reference As Date) As Integer

If BirthDay.AddYears(-BirthDay.Year + 1) _
<= Reference.AddYears(-Reference.Year + 1) Then
Return Reference.Year - BirthDay.Year
Else
Return Reference.Year - BirthDay.Year - 1
End If

End Function

Public Shared Function GetAge(ByVal BirthDay As Date) As Integer
Return GetAge(BirthDay, Date.Now)
End Function



Armin
 
Brian,

When you start using 365 than you are sure that you are not accurate,
because of the leap years with a Georgian calendar. The only accurate wat to
see how old somebody is, is in a simple way as far as I know, is doing it in
years.

\\\
Dim dt As DateTime = Now.AddYears(-32)
Dim years As Integer = Now.Year - dt.Year
MessageBox.Show(years.ToString)
///

However what you want is easy of course.
\\\
Dim dt As DateTime = Now.AddDays(-61)
Dim ts As TimeSpan = Now.Date.Subtract(dt.Date)
MessageBox.Show(ts.TotalDays.ToString)
///

I hope this helps,

Cor
 
Brian,
Rather then attempt to use DateDiff, I would consider writing my own
routine.

Something like:

Private Function CalculateAge(ByVal now As DateTime, ByVal birthday As
DateTime) As Integer
Dim age As Integer = now.Year - birthday.Year - 1
Dim currentBirthday As New DateTime(now.Year, birthday.Month,
birthday.Day)
If currentBirthday <= now.Date Then age += 1
Return age
End Function

Then call it twice

Dim d_runOn As DateTime = #4/4/2005#

Dim d_bday As DateTime = #6/26/1986#

Debug.WriteLine(CalculateAge(d_runOn.AddMonths(-2), d_bday), "-2
months")
Debug.WriteLine(CalculateAge(d_runOn.AddMonths(2), d_bday), "+2
months")


There was a long discussion on this a couple of years ago in this newsgroup:

http://groups-beta.google.com/group...ef998/dcb82e0596271f6b?hl=en#dcb82e0596271f6b

Hope this helps
Jay


|I am trying to find someones age within 2 months of their birthday on the
| given date for the run of the application
|
| Dim d_runOn As DateTime = #4/4/2005#
|
| Dim d_bday As DateTime = #6/26/1986#
|
| Console.WriteLine(String.Format("Ran on {0}, birthdate {1}",
| d_runOn.ToShortDateString, d_bday.ToShortDateString))
|
| Console.WriteLine(String.Format("Years difference: {0}",
| DateDiff(DateInterval.Year, d_bday, d_runOn.AddMonths(2),
| FirstDayOfWeek.System, FirstWeekOfYear.System)))
|
| Console.WriteLine(String.Format("Age of date: {0}",
| Math.Floor((DateDiff(DateInterval.Day, d_bday, d_runOn.AddMonths(2),
| FirstDayOfWeek.System, FirstWeekOfYear.System) / 365))))
|
|
|
| of course the DateInteral.Year rounds so even though the person has no
| turned 19 yet, it says they are 19. I need it to say they are still 18
years
| old (the second one with the Math.Floor / 365 one) gives the correct age
| (18). How would i prevent rounding of the DateInterval.Year one?! or is it
| even possible?
|
|
 
doh,

I made my first showed sample very quick after the second (as an addition)
and see now from Jay's sample that I felt in what I see often in other
samples about dates.

When you need the years take than the one from Jay or any variation on that.

For those days, you can still take mine.

Cor
 
Back
Top