473,405 Members | 2,300 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,405 software developers and data experts.

Finding Age from DOB

Hi all,

Can anyone tell me how to find out the exact AGE from Date of
Birth ( DateTime) in C#?? I have tried with
DateTime.Now.Subtract(dob).But I coudlnt get the exact age.. Is there
any property available for this??? Plz help me....
Thanx,
James..

May 30 '06 #1
5 2606
<co*******@gmail.com> wrote in message
news:11*********************@j55g2000cwa.googlegro ups.com...
Can anyone tell me how to find out the exact AGE from Date of
Birth ( DateTime) in C#??


Depends what you mean by *exact* age... How accurate are you trying to be
here?
May 30 '06 #2
James,

Why cant u jus add a reference to VisualBasic and use Datediff method
to achieve the result?
Or in other case, we may need to write a custom(?) code to get the
age!!!
If u need further clarification, write me back @
Ve**********@polaris.co.in.

Thanks,
Venkat

May 30 '06 #3
co*******@gmail.com wrote:
Hi all,

Can anyone tell me how to find out the exact AGE from Date of
Birth ( DateTime) in C#?? I have tried with
DateTime.Now.Subtract(dob).But I coudlnt get the exact age.. Is there
any property available for this??? Plz help me....
Thanx,
James..

If I were born on 31st May 1946 then tomorrow I would be 60. Today I am
only 59 ;-)

DateTime dob = new DateTime(1946, 5, 31);
DateTime nn = new DateTime(2006, 5, 30);
TimeSpan age = nn - dob;
double ageInYears = age.Days / 365.25;

so I could either Math.Round(ageInYears) to get 60
or
Math.Truncate(ageInYears) to get 59.

Of course this example is a bit simple in that we are very close to 60.
Let the group know if this doesn't solve your particular problem - I'm
sure there are many ways to address this question.

Eddie
May 30 '06 #4
"Venkat" <si**********@gmail.com> wrote in message
news:11**********************@j33g2000cwa.googlegr oups.com...
Why cant u jus add a reference to VisualBasic and use Datediff method
to achieve the result?


There's no need whatsoever to do that - the TimeSpan object will do just as
well...
May 30 '06 #5
Nothing in-built that I know of. Leap years are always the trick
(technically not always 1-in-4) ... not hugely graceful, but the following
is at least easy to understand... (done this way as DayOfYear is not so
reliable in a leap year):

Marc

public static void Main() {
DateTime dob = new DateTime(1946, 5, 31);
DateTime today = DateTime.Today, tomorrow = today.AddDays(1);

Console.WriteLine(GetAge(dob, today));
Console.WriteLine(GetAge(dob, tomorrow));

}
public static int GetAge(DateTime dob, DateTime from) {
int years = from.Year - dob.Year;
if ((dob.Month > from.Month) || (dob.Month == from.Month && dob.Day
from.Day))

years--; // haven't had a birthday yet
return years;
}
May 30 '06 #6

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

Similar topics

1
by: Tristan | last post by:
Im trying to expand a search util by uing regular expression to allow common search criteria such as +-* and phrases "". My understanding of ereg(string pattern, string string, ) is that the...
11
by: Fuzzyman | last post by:
What's the best, cross platform, way of finding out the directory a script is run from ? I've googled a bit, but can't get a clear answer. On sys.argv the docs say : argv is the script name...
1
by: Phil Watkins | last post by:
I am a novice programer in Vb and I am having a major brain ache finding out which item has been selected within a list view and then either deleting that item or editing them. My searching so...
0
by: Helge Jensen | last post by:
Having posted in microsoft.public.dotnet.framework.sdk and microsoft.public.dotnet.framework.wmi without receiving any response, I posthere on the off-chance that someone who isn't following those...
3
by: KL | last post by:
Well, I am back. This time our assignment has us filling a vector and then timing how long it takes to find a spot in the vector to insert a new item, and the time required to insert the item...
8
by: Rick Strahl [MVP] | last post by:
Hi all, I'm building an app that uses the ASP.Net runtime... One problem I've run into is that pages running inside of the runtime are not finding DLLs in the GAC. In fact, if I look at the...
1
by: Doug | last post by:
The html below shows DataList "DiscountList" nested within DataList "EventItemList". DiscountList contains a Label control. I'm trying to find the label, using FindControl, during...
2
by: ElkGroveR | last post by:
Hi there! I'm using PHP to create a simple, dynamic MySQL SELECT query. The user chooses a selection from a HTML Form SELECT element's many options and submits the form via a POST action. ...
0
by: NSF12345 | last post by:
Iv developed a small program that looks for a file over our network, and copy it to the location of another computer. Im using the "If FileExists("\\oldpc\main share\Folder\file.txt") Then" way of...
4
by: krishnai888 | last post by:
I had already asked this question long back but no one has replied to me..I hope someone replies to me because its very important for me as I am doing my internship. I am currently writing a code...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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,...

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.