calculate age from date of birth

Discussion in 'ASP .Net' started by TJS, Nov 17, 2003.

  1. TJS

    TJS Guest

    what namespaces are required so I can do a simple calculate age from date of
    birth ?
     
    TJS, Nov 17, 2003
    #1
    1. Advertising

  2. System.DateTime

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    http://www.takempis.com
    Big Things are made up of
    Lots of Little Things.

    "TJS" <> wrote in message
    news:...
    > what namespaces are required so I can do a simple calculate age from date

    of
    > birth ?
    >
    >
    >
     
    Kevin Spencer, Nov 17, 2003
    #2
    1. Advertising

  3. TJS

    Chris Bower Guest

    In VB you can just use the DateDiff function, in C# use the TimeSpan
    structure.

    "TJS" <> wrote in message
    news:...
    > what namespaces are required so I can do a simple calculate age from date

    of
    > birth ?
    >
    >
    >
     
    Chris Bower, Nov 17, 2003
    #3
  4. TJS

    TJS Guest

    I added

    "Imports System.DateTime" to my vb file, but keep getting error on
    "datediff" when I try to compile ?




    "Chris Bower" <> wrote in message
    news:...
    > In VB you can just use the DateDiff function, in C# use the TimeSpan
    > structure.
    >
    > "TJS" <> wrote in message
    > news:...
    > > what namespaces are required so I can do a simple calculate age from

    date
    > of
    > > birth ?
    > >
    > >
    > >

    >
    >
     
    TJS, Nov 17, 2003
    #4
  5. TJS

    TJS Guest

    I added

    "Imports System.DateTime" to my vb file, but keep getting error on
    "datediff" when I try to compile ?


    "Kevin Spencer" <> wrote in message
    news:...
    > System.DateTime
    >
    > --
    > HTH,
    >
    > Kevin Spencer
    > Microsoft MVP
    > .Net Developer
    > http://www.takempis.com
    > Big Things are made up of
    > Lots of Little Things.
    >
    > "TJS" <> wrote in message
    > news:...
    > > what namespaces are required so I can do a simple calculate age from

    date
    > of
    > > birth ?
    > >
    > >
    > >

    >
    >
     
    TJS, Nov 17, 2003
    #5
  6. TJS

    Chris Bower Guest

    DateDiff is a function in the Microsoft.VisualBasic namespace.

    "TJS" <> wrote in message
    news:...
    > I added
    >
    > "Imports System.DateTime" to my vb file, but keep getting error on
    > "datediff" when I try to compile ?
    >
    >
    >
    >
    > "Chris Bower" <> wrote in message
    > news:...
    > > In VB you can just use the DateDiff function, in C# use the TimeSpan
    > > structure.
    > >
    > > "TJS" <> wrote in message
    > > news:...
    > > > what namespaces are required so I can do a simple calculate age from

    > date
    > > of
    > > > birth ?
    > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    Chris Bower, Nov 17, 2003
    #6
  7. TJS

    Chris Bower Guest

    Oh and System.DateTime is a structure, not a namespace.

    "TJS" <> wrote in message
    news:...
    > I added
    >
    > "Imports System.DateTime" to my vb file, but keep getting error on
    > "datediff" when I try to compile ?
    >
    >
    >
    >
    > "Chris Bower" <> wrote in message
    > news:...
    > > In VB you can just use the DateDiff function, in C# use the TimeSpan
    > > structure.
    > >
    > > "TJS" <> wrote in message
    > > news:...
    > > > what namespaces are required so I can do a simple calculate age from

    > date
    > > of
    > > > birth ?
    > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    Chris Bower, Nov 17, 2003
    #7
  8. TJS

    TJS Guest

    that worked

    thanks


    "Chris Bower" <> wrote in message
    news:...
    > DateDiff is a function in the Microsoft.VisualBasic namespace.
    >
    > "TJS" <> wrote in message
    > news:...
    > > I added
    > >
    > > "Imports System.DateTime" to my vb file, but keep getting error on
    > > "datediff" when I try to compile ?
    > >
    > >
    > >
    > >
    > > "Chris Bower" <> wrote in message
    > > news:...
    > > > In VB you can just use the DateDiff function, in C# use the TimeSpan
    > > > structure.
    > > >
    > > > "TJS" <> wrote in message
    > > > news:...
    > > > > what namespaces are required so I can do a simple calculate age from

    > > date
    > > > of
    > > > > birth ?
    > > > >
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    TJS, Nov 18, 2003
    #8
  9. And the error was?...

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    http://www.takempis.com
    Big Things are made up of
    Lots of Little Things.

    "TJS" <> wrote in message
    news:...
    > I added
    >
    > "Imports System.DateTime" to my vb file, but keep getting error on
    > "datediff" when I try to compile ?
    >
    >
    >
    >
    > "Chris Bower" <> wrote in message
    > news:...
    > > In VB you can just use the DateDiff function, in C# use the TimeSpan
    > > structure.
    > >
    > > "TJS" <> wrote in message
    > > news:...
    > > > what namespaces are required so I can do a simple calculate age from

    > date
    > > of
    > > > birth ?
    > > >
    > > >
    > > >

    > >
    > >

    >
    >
     
    Kevin Spencer, Nov 18, 2003
    #9
  10. TJS

    gosborne

    Joined:
    Sep 7, 2010
    Messages:
    1
    Some extension methods that may help

    Here are some extension methods I frequently use when needing to calculate age. The first two extend TimeSpan to add a Years and TotalYears value to TimeSpan, the second extends a DateTime to give an Age (int) when passed a date to calculate the age from.

    Code:
    Namespace MyFunctions
        static class MyExtensions
    
            private const double YEAR_CALC = 365.2425;
    
            public static int Years(this TimeSpan ts) {
                return (int)Math.Floor(ts.TotalYears());
            }
            public static double TotalYears(this TimeSpan ts) {
                return ((double)ts.Days) / YEAR_CALC;
            }
            public static int Age(this DateTime dob, DateTime fromDate) {
                TimeSpan ts = fromDate.Subtract(dob);
                int age = ts.Years();
                [COLOR="green"]//will never return negative years - if this is desired, [/COLOR]
                [COLOR="green"]// just return age[/COLOR]
                return Math.Abs(age);
            }
        }
    }
    To use these, just add the following to the top of your class and the methods will be available on each of those types.

    Code:
    using MyFunctions.MyExtensions;
     
    gosborne, Sep 7, 2010
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?iso-8859-1?B?bW9vcJk=?=
    Replies:
    7
    Views:
    890
    Roedy Green
    Jan 2, 2006
  2. cylin
    Replies:
    6
    Views:
    597
    Mike Wahler
    Aug 19, 2003
  3. Rick North

    Birth date for VHDL 87 ?

    Rick North, Jan 19, 2007, in forum: VHDL
    Replies:
    2
    Views:
    448
    Rick North
    Jan 20, 2007
  4. Gav

    Finding Age from Date of Birth

    Gav, Jan 4, 2004, in forum: ASP General
    Replies:
    20
    Views:
    425
    Guest
    Jan 5, 2004
  5. eggie5

    Age to birth year

    eggie5, Sep 25, 2007, in forum: Ruby
    Replies:
    2
    Views:
    117
    Jan Friedrich
    Sep 25, 2007
Loading...

Share This Page