Calculating age from DOB

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

    Calculating age from DOB

    Hi

    How can I calculate age from a given DOB?

    Thanks

    Regards


  • =?Utf-8?B?RmFtaWx5IFRyZWUgTWlrZQ==?=

    #2
    RE: Calculating age from DOB



    "John" wrote:
    Hi
    >
    How can I calculate age from a given DOB?
    >
    Thanks
    >
    Regards
    >
    >
    I would use something akin to the following:

    Sub Main()
    Dim DOB As Date = New Date(1925, 8, 4) ' use your favorite date
    Dim Today As Date = Date.Now

    Dim age As Integer = Today.Year - DOB.Year

    If (Today.Month < DOB.Month) Then age = age - 1
    If (Today.Month = DOB.Month) And (Today.Day < DOB.Day) Then _
    age = age - 1

    Console.WriteLi ne("Age: " & age)

    Console.ReadLin e()
    End Sub


    Hope this helps...

    >

    Comment

    • Spam Catcher

      #3
      RE: Calculating age from DOB

      =?Utf-8?B?RmFtaWx5IFR yZWUgTWlrZQ==?=
      <FamilyTreeMike @discussions.mi crosoft.comwrot e in news:8968EFD2-14D5-
      4AD4-8939-F50FEC7D936E@mi crosoft.com:
      I would use something akin to the following:
      >
      Sub Main()
      Dim DOB As Date = New Date(1925, 8, 4) ' use your favorite
      date
      Dim Today As Date = Date.Now
      >
      Dim age As Integer = Today.Year - DOB.Year
      >
      If (Today.Month < DOB.Month) Then age = age - 1
      If (Today.Month = DOB.Month) And (Today.Day < DOB.Day) Then _
      age = age - 1
      >
      Console.WriteLi ne("Age: " & age)
      >
      Console.ReadLin e()
      End Sub
      >
      >
      Hope this helps...
      >
      Can you just go Now.Subtract(DO B).Year?



      --
      spamhoneypot@ro gers.com (Do not e-mail)

      Comment

      • =?Utf-8?B?RmFtaWx5IFRyZWUgTWlrZQ==?=

        #4
        RE: Calculating age from DOB



        "Spam Catcher" wrote:
        Can you just go Now.Subtract(DO B).Year?
        >
        >
        That was my first thought, but my TimeSpan object doesn't have a Year
        property. :(


        Comment

        • Steve Gerrard

          #5
          Re: Calculating age from DOB

          Family Tree Mike wrote:
          "Spam Catcher" wrote:
          >
          >Can you just go Now.Subtract(DO B).Year?
          >>
          >>
          >
          That was my first thought, but my TimeSpan object doesn't have a Year
          property. :(
          Probably just as well, it wouldn't know how many years X number of days is
          anyway. :(

          If you want a one line version, and don't mind adding a boolean expression, you
          could write

          Dim age As Integer = Now.Year - DOB.Year + (New Date(Now.Year, DOB.Month,
          DOB.Day) Now)


          Comment

          • Spam Catcher

            #6
            RE: Calculating age from DOB

            =?Utf-8?B?RmFtaWx5IFR yZWUgTWlrZQ==?=
            <FamilyTreeMike @discussions.mi crosoft.comwrot e in news:0D3CEEB1-F499-
            4358-96A0-CC2328349D49@mi crosoft.com:
            "Spam Catcher" wrote:
            >
            >Can you just go Now.Subtract(DO B).Year?
            >>
            >>
            >
            That was my first thought, but my TimeSpan object doesn't have a Year
            property. :(
            >
            Ah you're right!

            Stupid me.

            --
            spamhoneypot@ro gers.com (Do not e-mail)

            Comment

            • =?Utf-8?B?U3VydHVyWg==?=

              #7
              RE: Calculating age from DOB

              You know, I was going to suggest DateDiff, but it behaves weirdly.

              Comment

              • Chris Dunaway

                #8
                Re: Calculating age from DOB

                On Mar 9, 4:00 pm, "Steve Gerrard" <mynameh...@com cast.netwrote:
                Family Tree Mike wrote:
                "Spam Catcher" wrote:
                >
                Can you just go Now.Subtract(DO B).Year?
                >
                That was my first thought, but my TimeSpan object doesn't have a Year
                property. :(
                >
                Probably just as well, it wouldn't know how many years X number of days is
                anyway. :(
                >
                If you want a one line version, and don't mind adding a boolean expression, you
                could write
                >
                Dim age As Integer = Now.Year - DOB.Year + (New Date(Now.Year, DOB.Month,
                DOB.Day) Now)
                And this compiles with option strict on? It makes no sense to add a
                boolean to an integer!

                Chris

                Comment

                • Steve Gerrard

                  #9
                  Re: Calculating age from DOB

                  Chris Dunaway wrote:
                  On Mar 9, 4:00 pm, "Steve Gerrard" <mynameh...@com cast.netwrote:
                  >If you want a one line version, and don't mind adding a boolean
                  >expression, you could write
                  >>
                  >Dim age As Integer = Now.Year - DOB.Year + (New Date(Now.Year,
                  >DOB.Month, DOB.Day) Now)
                  >
                  And this compiles with option strict on? It makes no sense to add a
                  boolean to an integer!
                  >
                  For option strict, you would need
                  Dim age As Integer = Now.Year - DOB.Year + CInt(New Date(Now.Year,
                  DOB.Month, DOB.Day) Now)

                  I didn't say I would go about adding booleans to integers, only that you could
                  if you wanted it all in one expression.;-)




                  Comment

                  Working...