Checks if it’s your birthday and if not, it will tell you how many days until your birthday

https://www.codecademy.com/courses/learn-c-sharp/lessons/csharp-conditional-statements/exercises/review

Can i anyone can help me the thing is the programming exercise is about checking your birthday if it is than print out “Happy Birthday” and if it’s not it will tell you how many days until your birthday.
i tried so many methods nothing work there here after extensive research the time span can show me difference but what should i do that its show me the difference between month and days not year.

using System;
namespace std
{
class Program
{
public static void Main(stringargs)
{
DateTime myBirthday = new DateTime(1994, 11, 5);
DateTime currentDate=DateTime.Now;
TimeSpan difference = currentDate - myBirthday;
if (myBirthday==currentDate)
{
Console.WriteLine(“Happy Birthday”);
}
else if(myBirthday!=currentDate)
{
Console.WriteLine(difference);
}
else
{
Console.WriteLine(“Invalid input”);
}
}
}
}

Hi,
The problem you’ve got is DateTime contains a time portion to it, so your myBirthday will have a time part set to 12:00:00 by default. This means any comparison with DateTime.Now as it is, is going to be wrong (unless, you try it at midnight and get lucky).
Your difference is also being calculated over a number of years, where you’re really only interested in the difference between your birthday this year and today.

DateTime does have a DayOfYear property which will give you an int value of the day of the year;
(e.g. feb 5 would be 36)
It might be easier to compare the dates using this;

eg. something like;

int difference = myBirthday.DayOfYear - currentDate.DayOfYear;
if (difference < 0)
{
difference += 365;
}
if (difference == 0)
{
Console.WriteLine("Happy birthday");
}
else
{
Console.WriteLine($"You've {difference} days until your next birthday");
}

Tbh, looking where it is in the course, I presume the intention was just to use ints for day and month and working it all out manually, so kudos for thinking out the box and looking up a better way.

hope that helps
(feel free to ask if you’re still not sure)

Thank you very much for your time its help alot…

1 Like