Reputation: 33990
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)?
Upvotes: 15
Views: 27011
Reputation: 150
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();
}
}
}
Upvotes: 0
Reputation: 333
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.
Upvotes: 2
Reputation: 3438
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.
Upvotes: -1
Reputation: 171794
// 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;
Upvotes: 30
Reputation: 26408
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.
Upvotes: 6
Reputation: 4991
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
Upvotes: -1