Calculating Age

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ron Vecchi

    #1

    Calculating Age

    I need to calculate the age of a person based on a DateTime BirthDate



    I was thinking
    TimeSpan ts = DateTime.Now - BirthDate;

    //I can get the days but not years.
    // I could check each year from their year of birth, count the days in the
    year and compare with the days returned from timespan

    but isn't there a easy way to check the difference in years?


    Thanks,
    Ron Vecchi


  • David Sworder

    #2
    Re: Calculating Age

    Maybe something like this?



    "Ron Vecchi" <vencenzo@comca st.net> wrote in message
    news:ufs8vf3WDH A.1492@TK2MSFTN GP12.phx.gbl...[color=blue]
    > I need to calculate the age of a person based on a DateTime BirthDate
    >
    >
    >
    > I was thinking
    > TimeSpan ts = DateTime.Now - BirthDate;
    >
    > //I can get the days but not years.
    > // I could check each year from their year of birth, count the days in[/color]
    the[color=blue]
    > year and compare with the days returned from timespan
    >
    > but isn't there a easy way to check the difference in years?
    >
    >
    > Thanks,
    > Ron Vecchi
    >
    >[/color]


    Comment

    • Jeff Lindholm

      #3
      Re: Calculating Age

      DateTime.Now.Ye ar - BirthDate.Year -

      (BirthDate.Mont h > DateTime.Now.Mo nth ? 1

      : (BirthDate.Mont h != DateTime.Now.Mo nth) ? 0

      : (BirthDate.Day > DateTime.Now.Da y) ? 1 : 0);

      "Ron Vecchi" <vencenzo@comca st.net> wrote in message
      news:ufs8vf3WDH A.1492@TK2MSFTN GP12.phx.gbl...[color=blue]
      > I need to calculate the age of a person based on a DateTime BirthDate
      >
      >
      >
      > I was thinking
      > TimeSpan ts = DateTime.Now - BirthDate;
      >
      > //I can get the days but not years.
      > // I could check each year from their year of birth, count the days in[/color]
      the[color=blue]
      > year and compare with the days returned from timespan
      >
      > but isn't there a easy way to check the difference in years?
      >
      >
      > Thanks,
      > Ron Vecchi
      >
      >[/color]


      Comment

      • Val Savvateev

        #4
        Re: Calculating Age

        The problem with calculating the difference in years is that the legth of a
        year is not a definite timespan (actual number of days in a year depends on
        its number). Same problem is applicable to months... (As the documentation
        states : "Due to a varying number of days in months and years, the longest
        unit of time that is used by TimeSpan is the day.")


        "Ron Vecchi" <vencenzo@comca st.net> wrote in message
        news:ufs8vf3WDH A.1492@TK2MSFTN GP12.phx.gbl...[color=blue]
        > I need to calculate the age of a person based on a DateTime BirthDate
        >
        >
        >
        > I was thinking
        > TimeSpan ts = DateTime.Now - BirthDate;
        >
        > //I can get the days but not years.
        > // I could check each year from their year of birth, count the days in[/color]
        the[color=blue]
        > year and compare with the days returned from timespan
        >
        > but isn't there a easy way to check the difference in years?
        >
        >
        > Thanks,
        > Ron Vecchi
        >
        >[/color]


        Comment

        Working...