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)?
The birthday age calculator calculates the remaining days for your next coming birthday when you select your date of birth. The birthday age calculator also tells you about your heartbeat, sleeping time, and the number of times you have laughed from the day you have taken birth.
Use our birthday calculator to work out the number of days until your next birthday. We calculate this based upon your birth date and today's date. On what day was I born? Use the birthday calculator to find out how many hours, days, months and years you've been alive for and what day you were born on.
If you need to calculate days remaining from today, use the TODAY function like so: The TODAY function will always return the current date. Note that after the end_date has passed, you'll start to see negative results, because the value returned by TODAY will be greater than the end date.
Count days until next birthday with formulas. 1 Step 2. Get today’s date. Now we need to get Today’s date with formula. Select cell B2, enter the below formula into it, and press Enter ley. Then you ... 2 Step 3. Get the date of next birthday. 3 Step 4. Get the remaining days until next birthday.
// 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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With