How to find age from date of birth in c#

how to find age from date of birth in c#
This appears to handle different regions date formats the best. Jun 25 '10 at 9: I don't think any of the answers so far provide for cultures that calculate age differently.

Replies 6 Email updates Last post was 29 Jan at Reply 10 years ago.

Plz reply any one Know this code. Advance thax for u'r answer Get the current date, subtract 26 years, subtract 4 months, subtract 15 days. There's you date of birth. You would actually subtract 26 years by adding years. Post a reply Enter your message below Sign in or Join us it's free.

How to calculate age from date of birth

Keeping Your Tests Legible. Quick links Most recent Most popular Unanswered My threads.

how to find age from date of birth in c#

Recent activity arif ahmad replied to How to receive data in web Add your solution here. Treat my content as plain text, not as HTML. Existing Members Sign in to your account. This email is in use. Do you need your password?

How to calculate age (in years) based on Date of Birth in SQL

When answering a question please: Read the question carefully. Not only us this system used primarily in Korea, but as a tourist discussing ages with locals, locals will politely refer to yourself an each other by their birth year. I'm not 25, I'm I like this approach better. In Vietnam, in daily life even when Western system is used, age is calculated merely by subtracting the years.

how to find age from date of birth in c#

By this you can obtain the age in the simplest possible way: DateTime object on a later date. I spot tested up to age I don't think this will work in general. I don't think Harry Patch would have appreciated your spot-testing methodology: The average length of a year in the Gregorian Calendar is In my testing this fails on the persons birthday, it reports them younger than they are.

Another function, not by me but found on the web and refined it a bit: This appears to handle different regions date formats the best.

How to Calculate Age from a Date of Birth using C#.Net?

I am late to the party, but here's a one-liner: Calculate Exact age - in years, months, days, etc. Solution for 1 is obvious: People also usually use it, when they calculate their age "manually": This is my preferred solution We cannot use DateTime.

DayOfYear or TimeSpans, as they shift number of days in leap years I have put there little more lines for readability Just one more note I would create 2 static overloaded methods for it, one for universal usage, second for usage-friendliness: Consider the age of a person born on March 1, on February 29, To rectify this, you need to do a lexicographic comparison of Month, DayOfMonth pairs and use that for the conditional. So let's handle the others first. What to do now?

Learn, Share, Build

The best way that I know of because of leap years and everything is: This gives "more detail" to this question. This does not work all the time. Adding a Span to the DateTime. MinValue could work boes this does not account for leap years etc.

I have asked on microsoft forums and microsoft has confirmed it takes into account leap years between 2 dates. Consider the following TWO senarios.

how to find age from date of birth in c#

In the second senarion DateTime. In this case the result will be 1 years, 0 months and 0 days. That will happen because you are adding the timespan on a non-leap year.

how to find age from date of birth in c#

MinValue was a leap year then the results would be 1 year at the first and 0 years 11 months and 30 days. Try it in your code. I came up with a solution that is pretty much identical I used DateTime.

Calculate Date of Birth from Age

You are quite right it's not. But IF it was that would be the result. Why does it matter? In either case leap or not then there are examples where this does not work.

That was what I wanted to show. The DIFF is correct. Span takes into account leap years.

how to find age from date of birth in c#

Try the examples in code and you will see I'm right. This is useful when you need it as part of a query: I've tested against the Feb 29th problem and leap years and it seems to work, I'd appreciate any feedback: Month, 1 ; while tmpMyDOB.

I'm afraid we don't have any feedback for you just yet. Keeping it simple and possibly stupid: Now - birth; Console. TimeSpan was my first choice, but found that it doesn't offer a TotalYears property. You could try ts. Here is a solution. Month ; else if reference. For instance, per Wikipediaif you're born on February 29th in various jurisdictions, you're non-leap year birthday varies: In the United Kingdom and Hong Kong: To that end, an improvement: AddDays -1 ; break; default: A western European reckoning of age, and A calendar, like the Gregorian calendar that inserts a single leap day at the end of a month.

Twenty C# Questions Explained, 14, How do I calculate someone's age in C#

You will meet Exotic Russian Brides after Free Registration!

First Name