Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > calculate age from date of birth

Reply
Thread Tools

calculate age from date of birth

 
 
TJS
Guest
Posts: n/a
 
      11-17-2003
what namespaces are required so I can do a simple calculate age from date of
birth ?



 
Reply With Quote
 
 
 
 
Kevin Spencer
Guest
Posts: n/a
 
      11-17-2003
System.DateTime

--
HTH,

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

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

of
> birth ?
>
>
>



 
Reply With Quote
 
 
 
 
Chris Bower
Guest
Posts: n/a
 
      11-17-2003
In VB you can just use the DateDiff function, in C# use the TimeSpan
structure.

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

of
> birth ?
>
>
>



 
Reply With Quote
 
TJS
Guest
Posts: n/a
 
      11-17-2003
I added

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




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

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

>
>



 
Reply With Quote
 
TJS
Guest
Posts: n/a
 
      11-17-2003
I added

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


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

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

>
>



 
Reply With Quote
 
Chris Bower
Guest
Posts: n/a
 
      11-17-2003
DateDiff is a function in the Microsoft.VisualBasic namespace.

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

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

> >
> >

>
>



 
Reply With Quote
 
Chris Bower
Guest
Posts: n/a
 
      11-17-2003
Oh and System.DateTime is a structure, not a namespace.

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

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

> >
> >

>
>



 
Reply With Quote
 
TJS
Guest
Posts: n/a
 
      11-18-2003
that worked

thanks


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

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

> >
> >

>
>



 
Reply With Quote
 
Kevin Spencer
Guest
Posts: n/a
 
      11-18-2003
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> I added
>
> "Imports System.DateTime" to my vb file, but keep getting error on
> "datediff" when I try to compile ?
>
>
>
>
> "Chris Bower" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > In VB you can just use the DateDiff function, in C# use the TimeSpan
> > structure.
> >
> > "TJS" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> > > what namespaces are required so I can do a simple calculate age from

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

> >
> >

>
>



 
Reply With Quote
 
gosborne gosborne is offline
Junior Member
Join Date: Sep 2010
Posts: 1
 
      09-07-2010
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();
            //will never return negative years - if this is desired, 
            // just return age
            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;
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Age to birth year eggie5 Ruby 2 09-25-2007 05:56 PM
calculate the age from the selectdate =?Utf-8?B?am9l?= ASP .Net 2 03-30-2006 08:01 AM
print("my name is {name}, and {age}-year old {gender}", name, age, gender); =?iso-8859-1?B?bW9vcJk=?= Java 7 01-02-2006 04:39 PM
Finding Age from Date of Birth Gav ASP General 20 01-05-2004 09:01 AM
Can cout do like printf("Name: %s, Age: %d\n",name,age)? cylin C++ 6 08-19-2003 09:03 PM



Advertisments