1

Possible Duplicate:
How do I calculate someone's age in C#?

how do I calculate what a persons age will be on a given date?

I have the date of birth, and for what I am doing now, the date I want to calculate it for is the end of the year e.g. 31/12/2012 23:59:59.

How can I calculate from these variables how many years old a person will be at the end of the year?

Thanks.

Community
  • 1
  • 1
Remotec
  • 10,304
  • 25
  • 105
  • 147
  • After testing this I fail to see how this is a duplicate. I have checked the accepted answer in the "duplicate" question and it does not work in this case. I'm not calculating someones age - I want to know how old they are at a certain point in time. This is a different question with a different solution. – Remotec Feb 28 '12 at 11:19
  • 1
    @RemotexUk, using the answer in the other question will work, just replace `now` with whatever date you need. – Ash Burlaczenko Mar 02 '12 at 07:55

6 Answers6

1

Subtract the years from the target date and the birth date; subtract 1 year if the target date falls before the birth date in that year.

duffymo
  • 305,152
  • 44
  • 369
  • 561
1
DateTime birthDayPersonA = new DateTime(1986,12,1);
DateTime givenDate = new DateTime(2012,12,24);
TimeSpan age = givenDate.Substract(birthDayPersonA);
RvdK
  • 19,580
  • 4
  • 64
  • 107
1

You can do the following:

  1. Create DateTime instance with the date of birth
  2. Create DateTime instance with the destination (e.g. 31/12/2012 23:59:59);
  3. Use the Subtract method on the second DateTime instance to get the years between this two dates and add it to the user age
udidu
  • 8,269
  • 6
  • 48
  • 68
0
var age = new Timespan(endOfYearDate.Ticks - dateOfBirth.Ticks).TotalDays();

or just days if you want it to round for you.

NOTE you will need to convert days to years by dividing by 365.25

undefined
  • 33,537
  • 22
  • 129
  • 198
  • Just converting days to years like this is not very precise. – Dennis Traub Feb 28 '12 at 10:49
  • Yeah you're absolutely right it isn't, this will only work if you dont care too much about precision, otherwise you will need to map to how many leap years are in the timespan. This method may be 1/4 of a day wrong on a fractional year. – undefined Feb 28 '12 at 10:54
  • Leap years don't matter if you just retrieve the years from the timespan like in my answer above. Edit: What I stated is wrong, my method has the same problem, thanks for the hint. – Dennis Traub Feb 28 '12 at 10:58
  • This is actually a pretty interesting problem, ill write a better solution to this and post it somewhere. – undefined Feb 28 '12 at 11:11
0
var birthday = new DateTime(1973, 7, 10);
var date = new DateTime(2012, 12, 31);

TimeSpan span = date - birthday;

DateTime age = DateTime.MinValue + span;

// MinValue is 1/1/1 so we have to subtract one year
int yearsOfAge = age.Year - 1;
Dennis Traub
  • 50,557
  • 7
  • 93
  • 108
  • This method actually has exactly the same problem as mine with leap years, it doesn't place the leap year in the correct place so may be up to 1 day off. this could in a rare circumstance cause the year to be offset by 1 – undefined Feb 28 '12 at 10:57
  • Really? Hmm, I'll try and check with a few test cases. Edit: Yes, you're right. – Dennis Traub Feb 28 '12 at 10:59
  • Conciser if the person was born the year before a leap year on the 31st of dec, the end of this year is a leap year, and year 1 is a leap year, you have 1 too many leap years in your representation which will cause an off by 1. Its pretty rare but still possible – undefined Feb 28 '12 at 11:03
0

VB has DataDiff, C# doesn't exactly. Use the below classes

public enum DateInterval
    {
        Day,
        DayOfYear,
        Hour,
        Minute,
        Month,
        Quarter,
        Second,
        Weekday,
        WeekOfYear,
        Year
    }

    public class DateAndTime
    {
        public static long DateDiff(DateInterval interval, DateTime dt1, DateTime dt2)
        {
            return DateDiff(interval, dt1, dt2, System.Globalization.DateTimeFormatInfo.CurrentInfo.FirstDayOfWeek);
        }

        private static int GetQuarter(int nMonth)
        {
            if (nMonth <= 3)
                return 1;
            if (nMonth <= 6)
                return 2;
            if (nMonth <= 9)
                return 3;
            return 4;
        }

        public static long DateDiff(DateInterval interval, DateTime dt1, DateTime dt2, DayOfWeek eFirstDayOfWeek)
        {
            if (interval == DateInterval.Year)
                return dt2.Year - dt1.Year;

            if (interval == DateInterval.Month)
                return (dt2.Month - dt1.Month) + (12 * (dt2.Year - dt1.Year));

            TimeSpan ts = dt2 - dt1;

            if (interval == DateInterval.Day || interval == DateInterval.DayOfYear)
                return Round(ts.TotalDays);

            if (interval == DateInterval.Hour)
                return Round(ts.TotalHours);

            if (interval == DateInterval.Minute)
                return Round(ts.TotalMinutes);

            if (interval == DateInterval.Second)
                return Round(ts.TotalSeconds);

            if (interval == DateInterval.Weekday)
            {
                return Round(ts.TotalDays / 7.0);
            }

            if (interval == DateInterval.WeekOfYear)
            {
                while (dt2.DayOfWeek != eFirstDayOfWeek)
                    dt2 = dt2.AddDays(-1);
                while (dt1.DayOfWeek != eFirstDayOfWeek)
                    dt1 = dt1.AddDays(-1);
                ts = dt2 - dt1;
                return Round(ts.TotalDays / 7.0);
            }

            if (interval == DateInterval.Quarter)
            {
                double d1Quarter = GetQuarter(dt1.Month);
                double d2Quarter = GetQuarter(dt2.Month);
                double d1 = d2Quarter - d1Quarter;
                double d2 = (4 * (dt2.Year - dt1.Year));
                return Round(d1 + d2);
            }

            return 0;

        }

        private static long Round(double dVal)
        {
            if (dVal >= 0)
                return (long)Math.Floor(dVal);
            return (long)Math.Ceiling(dVal);
        }
    }

usage would be

public long HowOldAmIToday(DateTime DOB)
{
    return DateAndTime.DateDiff(DateInterval.Year, DOB, DateTime.Today);
}
MagicalArmchair
  • 911
  • 1
  • 12
  • 26