473,397 Members | 1,969 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

DateDiff rounds?

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?
Nov 21 '05 #1
4 3296

"Brian Henry" <no****@nospam.com> schrieb im Newsbeitrag
news:ub**************@tk2msftngp13.phx.gbl...
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

Nov 21 '05 #2
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
Nov 21 '05 #3
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/...b82e0596271f6b

Hope this helps
Jay
"Brian Henry" <no****@nospam.com> wrote in message
news:ub**************@tk2msftngp13.phx.gbl...
|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?
|
|
Nov 21 '05 #4
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
Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: CJM | last post by:
I have an ASP page that lists files and folders in a directory. I'm using a cookie to record the last time this page was visited, and I intend to show links that are created/modified from that date...
8
by: inamori | last post by:
I face that problems 07/01/2003 06/30/2006 ---------> it should be 3 01/01/2003 02/28/2005 --------->could i get 2 years and 2 months 01/01/2003 03/01/2005 ...
6
by: Lofty | last post by:
Hi all. I have to write an app that interacts with mySQL (I really must have done some evil, evil stuff in a previous life to be landed with this!) I need to work out the difference in days...
1
by: intl04 | last post by:
I'm trying to set up a query that will include a new field ('Days until completion') whose value is derived from the DateDiff function. I think I have the syntax correct but am not sure. Days...
4
by: Paolo | last post by:
I am having some problem with a Year Function. I have form on which I have 4 field which indicate dates and an additional form which sums those dates: These are the fields: YEARS...
1
by: PMBragg | last post by:
ORINGINAL Post >Thank you in advance. I'm trying to pull all inventory items from December >of the previous year back to 4 years for my accountant. I know this can be >done, but I'm drawing a...
7
by: Adrian | last post by:
I hit on this problem converting a VB.NET insurance application to C#. Age next birthday calculated from date of birth is often needed in insurance premium calculations. Originally done using...
6
by: kevinjwilson | last post by:
I am trying to get the date difference between two dates but I don't want the function to include weekends in the calculation. Does anyone have an idea on how to make this work?
2
by: muddasirmunir | last post by:
i am using vb 6 , i had place two datepicker in form now i want to calcuate differcen of month in two date for this i used the function datediff i had try it withh many syntax but getting error...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.