DateDiff rounds?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brian Henry

    #1

    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.WriteLi ne(String.Forma t("Ran on {0}, birthdate {1}",
    d_runOn.ToShort DateString, d_bday.ToShortD ateString))

    Console.WriteLi ne(String.Forma t("Years difference: {0}",
    DateDiff(DateIn terval.Year, d_bday, d_runOn.AddMont hs(2),
    FirstDayOfWeek. System, FirstWeekOfYear .System)))

    Console.WriteLi ne(String.Forma t("Age of date: {0}",
    Math.Floor((Dat eDiff(DateInter val.Day, d_bday, d_runOn.AddMont hs(2),
    FirstDayOfWeek. System, FirstWeekOfYear .System) / 365))))



    of course the DateInteral.Yea r 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.Ye ar one?! or is it
    even possible?


  • Armin Zingler

    #2
    Re: DateDiff rounds?


    "Brian Henry" <nospam@nospam. com> schrieb im Newsbeitrag
    news:ubukeCHXFH A.1468@tk2msftn gp13.phx.gbl...[color=blue]
    >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.WriteLi ne(String.Forma t("Ran on {0}, birthdate {1}",
    > d_runOn.ToShort DateString, d_bday.ToShortD ateString))
    >
    > Console.WriteLi ne(String.Forma t("Years difference: {0}",
    > DateDiff(DateIn terval.Year, d_bday, d_runOn.AddMont hs(2),
    > FirstDayOfWeek. System, FirstWeekOfYear .System)))
    >
    > Console.WriteLi ne(String.Forma t("Age of date: {0}",
    > Math.Floor((Dat eDiff(DateInter val.Day, d_bday, d_runOn.AddMont hs(2),
    > FirstDayOfWeek. System, FirstWeekOfYear .System) / 365))))
    >
    >
    >
    > of course the DateInteral.Yea r 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.Ye ar one?! or is it even possible?[/color]

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

    If BirthDay.AddYea rs(-BirthDay.Year + 1) _
    <= Reference.AddYe ars(-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

    Comment

    • Cor Ligthert

      #3
      Re: DateDiff rounds?

      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.Subtra ct(dt.Date)
      MessageBox.Show (ts.TotalDays.T oString)
      ///

      I hope this helps,

      Cor


      Comment

      • Jay B. Harlow [MVP - Outlook]

        #4
        Re: DateDiff rounds?

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

        Something like:

        Private Function CalculateAge(By Val now As DateTime, ByVal birthday As
        DateTime) As Integer
        Dim age As Integer = now.Year - birthday.Year - 1
        Dim currentBirthday As New DateTime(now.Ye ar, 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.AddMonth s(-2), d_bday), "-2
        months")
        Debug.WriteLine (CalculateAge(d _runOn.AddMonth s(2), d_bday), "+2
        months")


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



        Hope this helps
        Jay


        "Brian Henry" <nospam@nospam. com> wrote in message
        news:ubukeCHXFH A.1468@tk2msftn gp13.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.WriteLi ne(String.Forma t("Ran on {0}, birthdate {1}",
        | d_runOn.ToShort DateString, d_bday.ToShortD ateString))
        |
        | Console.WriteLi ne(String.Forma t("Years difference: {0}",
        | DateDiff(DateIn terval.Year, d_bday, d_runOn.AddMont hs(2),
        | FirstDayOfWeek. System, FirstWeekOfYear .System)))
        |
        | Console.WriteLi ne(String.Forma t("Age of date: {0}",
        | Math.Floor((Dat eDiff(DateInter val.Day, d_bday, d_runOn.AddMont hs(2),
        | FirstDayOfWeek. System, FirstWeekOfYear .System) / 365))))
        |
        |
        |
        | of course the DateInteral.Yea r 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.Ye ar one?! or is it
        | even possible?
        |
        |


        Comment

        • Cor Ligthert

          #5
          Re: DateDiff rounds?

          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


          Comment

          Working...