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 769445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:20:12+00:00 2026-05-14T18:20:12+00:00

My application needs to adjust a clients current age by +0.5 if it has

  • 0

My application needs to adjust a clients current age by +0.5 if it has been 6 months since their last birthday.

The code should look something like this, but how many ticks would there be in 6 months?

if (DateTime.Today - dateOfBirth.Date > new TimeSpan(6))
        {
            adjust = 0.5M;
        }
        else
        {
            adjust = 0M;
        }

Thanks in advance

  • 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-14T18:20:12+00:00Added an answer on May 14, 2026 at 6:20 pm

    EDIT: You know what, actually? Since clearly what you really need is just to display the user’s age to within 6 months, this is what you should really do.

    static decimal GetApproximateAge(DateTime dateOfBirth) {
        TimeSpan age = DateTime.Now - dateOfBirth;
    
        /* a note on the line below:
         * treating 182.5 days as equivalent to 6 months,
         * reasoning that this will provide acceptable accuracy
         * for ages below ~360 years
         * 
         * (result will be 1 day off for roughly every 4 years;
         * for a calculation of half-years to be inaccurate
         * would take 4 [years] * ~90 [days] = ~360)
         */
    
        // get age in units of 6 months
        // desired behavior is not to add 0.5 until
        // a full six months have elapsed since the user's last birthday --
        // using Math.Floor to ensure this
        double approxAgeInHalfYears = Math.Floor(age.TotalDays / 182.5);
    
        // now convert this to years
        // did it this way to restrict age to increments of 0.5
        double approxAgeInYears = approxAgeInHalfYears * 0.5;
    
        return Convert.ToDecimal(approxAgeInYears);
    }
    

    The above code includes a big comment explaining its own shortcomings, helpfully pointed out by David.

    Now, here’s yet another option. It’s kind of ugly because it uses a loop; but it’s also more rock-solid since it utilizes the DateTime.AddMonths method, which has the advantage of having already been tested and documented by Microsoft.

    static decimal GetApproximateAge(DateTime dateOfBirth) {
        DateTime now = DateTime.Now;
    
        int birthYear = dateOfBirth.Year;
        int lastYear = now.Year - 1;
    
        // obviously, if the user's alive, he/she had a birthday last year;
        // therefore he/she is at least this old
        int minimumAgeInYears = lastYear - birthYear;
    
        // now the question is: how much time has passed since then?
        double actualAgeInYears = (double)minimumAgeInYears;
    
        // for every six months that have elapsed since the user's birthday
        // LAST year, add 0.5 to his/her age
        DateTime birthDateLastYear = new DateTime(lastYear, 1, 1)
            .AddDays(dateOfBirth.DayOfYear);
    
        DateTime comparisonDate = birthDateLastYear
            .AddMonths(6);
    
        while (comparisonDate < now) {
            actualAgeInYears += 0.5;
            comparisonDate = comparisonDate.AddMonths(6);
        }
    
        return Convert.ToDecimal(actualAgeInYears);
    }
    

    This idea that people have been suggesting of checking dateOfBirth.AddMonths(6) is wrong. Since dateOfBirth is a DateTime, it represents the user’s birth date, not their birth day.

    What you want to check is if six months have elapsed since the user’s last birthday–not the date they were born. Here’s one way to do that:

    DateTime lastBirthDay = GetLastBirthday(dateOfBirth);
    
    if (DateTime.Today > lastBirthDay.AddMonths(6))
    {
        adjust = 0.5M;
    }
    else
    {
        adjust = 0M;
    }
    
    DateTime GetLastBirthday(DateTime dateOfBirth)
    {
        int currentYear = DateTime.Now.Year;
        int birthMonth = dateOfBirth.Month;
        int birthDay = dateOfBirth.Day;
    
        // if user was born on Feb 29 and this year is NOT a leap year,
        // we'll say the user's birthday this year falls on Feb 28
        if (birthMonth == 2 && birthDay == 29 && !IsLeapYear(currentYear))
            birthDay = 28;
    
        DateTime birthdayThisYear = new DateTime(
            currentYear,
            birthMonth,
            birthDay
        );
    
        if (DateTime.Today > birthdayThisYear)
            return birthdayThisYear;
        else
            return birthdayThisYear.AddYears(-1);
    }
    
    bool IsLeapYear(int year) {
        return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

my application needs multiple jars to work. Since it is a desktop application i
My current application needs to get data from a file to initialize its attributes.
My application needs to send video data frame by frame from Server to Client.
My application needs to provide the same data in XML to 2 different providers
My application needs to use a couple of hard-coded symmetric cryptographic keys (while I
My application needs to know the path to a directory where it can store
Our application needs a simple scheduling mechanism - we can schedule only one visit
My application needs to display the time. Rather then displaying time as 11:00 I
My application needs control displaying bitmaps (jpg) but also zooming and panning them (so
My application needs to implement dynamic images, where a browse can get served a

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.