Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7601381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:09:05+00:00 2026-05-30T23:09:05+00:00

Hello so I’ve implemented this solution to attain the user’s birthday from a birthday

  • 0

Hello so I’ve implemented this solution to attain the user’s birthday from a birthday date input:
Calculate age in C#

This works great however I do need to interpret birthday’s for ages less than a year (babies, infants). It will current just give me an age of “0” if there are less than 365 days between the bdate and current date.

What I’m thinking is something like this:

public string calculateAge(DateTime birthDate, DateTime now)
        {
            //BDay is in different year (age > 1)
            int age = now.Year - birthDate.Year;
            if (now.Month < birthDate.Month || (now.Month == birthDate.Month && now.Day < birthDate.Day)) age--;

            if (age == 0)
            {
                //Bday is in same year
                age = now.Month - birthDate.Month;
                if (now.Month < birthDate.Month || (now.Month == birthDate.Month && now.Day < birthDate.Day)) age--;

                return age.ToString() + " months";
            }
            if (age == 0)
            {
                //Bday is in the same month
                age = now.Day - birthDate.Day;
                if (now.Month < birthDate.Month || (now.Month == birthDate.Month && now.Day < birthDate.Day)) age--;

                return age.ToString() + " days";
            }
            return age.ToString();
        }

However some of my test Bdays give me this:

(Today's date: 3/6/2012)
Bday1 = 3/5/2012
Age result = -1
Expected result = 1 day

Bday2 = 3/1/2012
Age result = 0 months
Expected result = 5 days

Bday3 = 1/1/2012
Age result = 2 months
Expected result = 2 months (this is fine)

Bday4 = 3/7/2011
Age result = -1 months
Expected result = 11 months

Bday5 = 3/1/2011
Age result = 1
Expected result = 1 (this is fine) 

You can see that because of how it is currently setup the issue is stemming from when the bday month is less than the current month some negative numbers can result.

I also see the error about not being able to get to the “days” loop, but I think that’s a moot point right now. Let me know if you have any insight on what I can do to get the desired results. Also if you need more info like test bdays too. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T23:09:06+00:00Added an answer on May 30, 2026 at 11:09 pm
    static public string calculateAge(DateTime birthDate, DateTime now)
    {
      birthDate = birthDate.Date;
      now = now.Date;
    
      var days = now.Day - birthDate.Day;
      if (days < 0)
      {
        var newNow = now.AddMonths(-1);
        days += (int)(now - newNow).TotalDays;
        now = newNow;
      }
      var months = now.Month - birthDate.Month;
      if (months < 0)
      {
        months += 12;
        now = now.AddYears(-1);
      }
      var years = now.Year - birthDate.Year;
      if (years == 0)
      {
        if (months == 0)
          return days.ToString() + " days";
        else
          return months.ToString() + " months";
      }
      return years.ToString();
    } 
    

    results (for now – 3/7/2012):

      3/5/2012: 2 days
      3/1/2012: 6 days
      1/1/2012: 2 months
      3/8/2011: 11 months
      3/1/2011: 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello i have a TextField on my scene. It haves only digits, user input
hello i wanted to build I get $date and $date1 from form xxx. I
Hello I have this problem with PyQt4-dev-tools that include: * a user interface compiler
Hello I have this query... if (isset($_REQUEST['deletePost'])) { $q = $dbc -> prepare(DELETE FROM
Hello again ladies and gents! OK, following on from my other question on ASP.NET
Hello can anybody solve this please I'm creating the object in the action class
Hello I am working with a simulator that uses rcS scripts to boot, this
Hello everyone i am trying to format the input number range with php number_format
hello friends i am trying to get a list of user id and want
Hello Guys I am trying to figure out why i am gettings this error

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.