15

I have a DateTime object with a person's birthday. I created this object using the person's year, month and day of birth, in the following way:

DateTime date = new DateTime(year, month, day);

I would like to know how many days are remaining before this person's next birthday. What is the best way to do so in C# (I'm new to the language)?

LarsTech
  • 80,625
  • 14
  • 153
  • 225
Roee Adler
  • 33,434
  • 32
  • 105
  • 133
  • 2
    @Mitch - sounds so but the full text search did not find a dupe in a few seconds so I thought I'd write the question in an articulate way... – Roee Adler Jul 23 '09 at 07:50
  • similar to http://stackoverflow.com/questions/9/how-do-i-calculate-someones-age-in-c – Russ Cam Jul 23 '09 at 15:09
  • @Russ - sorry but it's a different question. There are some similarities but it's certainly not the same (best proof is that the answers are different...) – Roee Adler Jul 23 '09 at 15:40

6 Answers6

30
// birthday is a DateTime containing the birthday

DateTime today = DateTime.Today;
DateTime next = new DateTime(today.Year,birthday.Month,birthday.Day);

if (next < today)
    next = next.AddYears(1);

int numDays = (next - today).Days;

This trivial algorithm fails if the birthday is Feb 29th. This is the alternative (which is essentially the same as the answer by Seb Nilsson:

DateTime today = DateTime.Today;
DateTime next = birthday.AddYears(today.Year - birthday.Year);

if (next < today)
    next = next.AddYears(1);

int numDays = (next - today).Days;
Philippe Leybaert
  • 168,566
  • 31
  • 210
  • 223
  • 1
    TotalDays returns a double : http://msdn.microsoft.com/en-us/library/system.timespan.totaldays.aspx – Mac Jul 23 '09 at 07:52
  • 1
    +1 This is close to the "manual" way I'm using right now, I thought maybe there's a neat C# trick to simplify it. – Roee Adler Jul 23 '09 at 07:52
  • +1, i thought he used DateTime.Now which includes the current time. – Leon Tayson Jul 23 '09 at 08:17
  • 2
    This will fail if the birthday is February 29 and the current year is not a leap year – Jesper Larsen-Ledet Oct 28 '11 at 21:36
  • @JesperLarsen-Ledet Indeed, I will correct my answer to handle this edge case – Philippe Leybaert Sep 10 '12 at 09:36
  • 2
    This is nice and elegant, but the handling for Feb 29th has one additional edge case that is missed. Consider the following conditions: birthday is Feb 29 and is past for the current year, the current year is not a leap year but the next year is. With the current logic above, the calculated "Days" will be one less than it should be as you'll be adding 1 year to Feb 28 even though it's for a leap year. See my answer for the slight one line tweak. – J Stuart May 06 '14 at 18:28
  • @JStuart You're right that it is one day off for the edge case you described, but the solution you proposed crashes in most cases when the birthday is a leap day – Philippe Leybaert May 06 '14 at 21:10
  • @PhilippeLeybaert Good catch - I was too myopic in my testing. I've updated my answer below with the additional check necessary to handle the scenario you described. – J Stuart May 06 '14 at 21:36
7

Using today's year and the birthday's month and day will not work with leap-years.

After a little bit of testing, this is what I get to work:

private static int GetDaysUntilBirthday(DateTime birthday) {
    var nextBirthday = birthday.AddYears(DateTime.Today.Year - birthday.Year);
    if(nextBirthday < DateTime.Today) {
        nextBirthday = nextBirthday.AddYears(1);
    }
    return (nextBirthday - DateTime.Today).Days;
}

Tested with 29th February on a leap-year and also when the birthday is the same day.

Seb Nilsson
  • 26,200
  • 30
  • 103
  • 130
  • 1
    This is the only methods that works 100%. The reason is that it uses `AddYears` which never fails for the ultimo dates of February. – Gustav Sep 15 '15 at 07:39
2

This is based off of Philippe Leybaert's answer above, but handles one additional edge case that I don't see accounted for in any of the previous answers.

The edge case I'm addressing is when the birthday is on a leap day, the birthday is in the past for the current year, and the current year isn't a leap year but the next year is.

The current answer provided will result in one fewer day as it sets "next" to Feb 28 of the current year and then adds a year making the date Feb 28 of a leap year (which isn't correct). Changing one line handles this edge case.

DateTime today = DateTime.Today;
DateTime next = birthday.AddYears(today.Year - birthday.Year);

if (next < today)
{
    if (!DateTime.IsLeapYear(next.Year + 1))
        next = next.AddYears(1);
    else
        next = new DateTime(next.Year + 1, birthday.Month, birthday.Day);
}

int numDays = (next - today).Days;

Update: Edited per Philippe's pointing out that my code had a rather sizable flaw.

J Stuart
  • 333
  • 2
  • 8
0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            DateTime dt1 = DateTime.Parse("09/08/2012");
            DateTime dt2 = DateTime.Parse(DateTime.Now.ToString());
            int days = (dt2 - dt1).Days;
            Console.WriteLine(days);

            double month = (dt2 - dt1).Days / 30;
            Console.WriteLine(month);
            double year = (dt2 - dt1).Days / 365;
            Console.WriteLine(year);
            Console.Read();
        }
    }
}
Mayur Narula
  • 150
  • 1
  • 4
-1

Try this method

private int GetDaysBeforeBirthday(DateTime birthdate)
{
    DateTime nextBday = new DateTime(DateTime.Now.Year, birthdate.Month, birthdate.Day);
    if (DateTime.Today > nextBday)
        nextBday = nextBday.AddYears(1);
    return (nextBday - DateTime.Today).Days;
}

just pass your birthdate and it will return the remaining days before your next birthday

Leon Tayson
  • 4,741
  • 7
  • 37
  • 36
-1
        DateTime Variable = DateTime.Now;
        int NumOfDaysTillNextMonth = 0;
        while (Variable < Comparer) //Comparer is just a target datetime
        {
            Variable = Variable.AddDays(1);
            NumOfDaysTillNextMonth++;
        }

Had to do this just now for a program. It's simple enough as opposed to the other methods if all you need is an integer of days left.

Adrian
  • 3,332
  • 5
  • 34
  • 52