locked
Date comparison in c# RRS feed

  • Question

  • I need to calculate a person age (day- for a baby - 60 days old) and year.

    I have birthdate as DateTime data type. 

    I know to get todays date with DateTime.Now.  I guess comparing can be done with "-" (substraction), but how can I get the result as integer.  (How many days old?  How many years old?)

    I tried this code, didn't work. :) I am not surprise though.  Doesn't look right.

    int age = int.Parse(DateTime.Now - birthdate);

     

    Thank you,

    Tuesday, November 29, 2011 8:48 AM

Answers

  • Hi

    Calculating someone's age is pretty straightforward, and here's how! In order for the code to work, you need a DateTime object called BirthDate containing the birthday.

    C#

    // get the difference in years
    int years = DateTime.Now.Year - BirthDate.Year;
    // subtract another year if we're before the
    // birth day in the current year
    if (DateTime.Now.Month < BirthDate.Month ||
    (DateTime.Now.Month == BirthDate.Month &&
    DateTime.Now.Day < BirthDate.Day))
    years--;

    VB.NET

    ' get the difference in years
    Dim years As Integer = DateTime.Now.Year - BirthDate.Year
    ' subtract another year if we're before the
    ' birth day in the current year
    If DateTime.Now.Month < BirthDate.Month Or (DateTime.Now.Month = BirthDate.Month And DateTime.Now.Day < BirthDate.Day) Then
    years = years - 1
    End If

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Tuesday, November 29, 2011 10:19 AM

All replies

  • Hi,

    first of all: You can get the days easily as you can subtract DateTimes using DateTime.Subtract (http://msdn.microsoft.com/en-us/library/8ysw4sby.aspx). This will give you a TimeSpan object which has a Days Property.

    To calculate the year, you should manually calculate the year through calculating it yourself: First you subtract the years and then you check, if the birthday date is before the current date (The current month is smalle the birthday month or ( the months are the same and the current day is smaller or equal the day of the birthday) ) in which case you have to subtract 1 of the difference.

    TimeSpan cannot give you a year or month because the number of days in a year of month is not fixed.

    With kind regards,

    Konrad

    Tuesday, November 29, 2011 9:01 AM
  • check this

    st->datetimeobj;

    int dtage = (now - st).Days;

    int age = new DateTime(DateTime.Now.Subtract(st).Ticks).Year - 1; or else divide the int dtage by 365.25

     

     



    Tuesday, November 29, 2011 9:36 AM
  • using System;
    
    namespace Demo
    {
        class Program
        {
            static void Main(string[] args)
            {
                DateTime dateOfBirth = new DateTime(1993, 12, 21);
                TimeSpan age = DateTime.Now - dateOfBirth;
                int ageInDays = (int) age.TotalDays;
                int ageInYears = AgeInYears(dateOfBirth, DateTime.Now);
                Console.WriteLine("Age in days = " + ageInDays);
                Console.WriteLine("Age in years = " + ageInYears);
            }
    
            public static int AgeInYears(DateTime birthday, DateTime today)
            {
                return ((today.Year - birthday.Year) * 372 + (today.Month - birthday.Month) * 31 + (today.Day - birthday.Day)) / 372;
            }
    
        }
    }
    
    

    • Proposed as answer by servy42 Friday, December 9, 2011 3:01 PM
    Tuesday, November 29, 2011 9:59 AM
  • Hi

    Calculating someone's age is pretty straightforward, and here's how! In order for the code to work, you need a DateTime object called BirthDate containing the birthday.

    C#

    // get the difference in years
    int years = DateTime.Now.Year - BirthDate.Year;
    // subtract another year if we're before the
    // birth day in the current year
    if (DateTime.Now.Month < BirthDate.Month ||
    (DateTime.Now.Month == BirthDate.Month &&
    DateTime.Now.Day < BirthDate.Day))
    years--;

    VB.NET

    ' get the difference in years
    Dim years As Integer = DateTime.Now.Year - BirthDate.Year
    ' subtract another year if we're before the
    ' birth day in the current year
    If DateTime.Now.Month < BirthDate.Month Or (DateTime.Now.Month = BirthDate.Month And DateTime.Now.Day < BirthDate.Day) Then
    years = years - 1
    End If

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Tuesday, November 29, 2011 10:19 AM
  • Thank you all for your answers.  I am dealing another problem right now.  As soon as I start to deal with this thing again, I will vote your answers.  Thank you again.
    Tuesday, November 29, 2011 3:48 PM
  • Hi mfataman,

    I temporarily mark Osama's reply as answer to push the case forward.

    If you have any questions, please feel free to let us know.

    You can start new threads in the forum as well.

    Thank you for your understanding and have a nice day!

    Yoyo.


    Yoyo Jiang[MSFT]
    MSDN Community Support | Feedback to us
    Friday, December 9, 2011 4:51 AM
    Moderator