1

how can I calculate if a date (in DateTime format) is 6 month later or not from my BirthDate (in DateTime format)?

DuffDan
  • 31
  • 8
  • Do you mean birthdate, or birthday? (i.e. do you want to know if a particular date is exactly 6 months after you were born, or 6 months after any of your birthdays?) – RB. Jan 06 '16 at 14:04
  • Please reread your question, it's really not clear what's the problem and what's expected. – andrew.fox Jan 06 '16 at 14:14

5 Answers5

7

Use DateTime AddMonth method

https://msdn.microsoft.com/ru-ru/library/system.datetime.addmonths(v=vs.110).aspx

var dat = new DateTime(2015, 12, 31);
var dat2 = new DateTime(2015, 12, 31);

if (dat.AddMonths(6) < dat2) { ... }
Valentin
  • 5,192
  • 2
  • 23
  • 37
  • dat.AddMonths(6) like this doesn't really do anything, as it RETURNS the date + 6 months, it doesn't actually change "dat". – André Kops Jan 06 '16 at 15:01
2

You should use DateTime.AddMonths :

DateTime dt;
DateTime birthDate;

if (dt <= birthDate.AddMonths(6))
{
}
w.b
  • 10,668
  • 5
  • 27
  • 47
1
DateTime birthDate=new DateTime(year,month,day);
DateTime dateToCompare = new DateTime(year, month, day);

if(dateToCompare >= birthdate.AddMonths(6))
{
   //DoSomething
}
Marshal
  • 6,401
  • 13
  • 53
  • 87
1

enter your birth date, calculate your next birthday and compare the dates,

        var born = new DateTime(1900, 02, 01);
        var checkdate = DateTime.Now;


        var nextBirthday = new DateTime(DateTime.Now.Year, born.Month, born.Day);
        if (nextBirthday < DateTime.Now)
        {
            nextBirthday = new DateTime(DateTime.Now.Year + 1, born.Month, born.Day);
        }

        if (checkdate.AddMonths(6) < nextBirthday)
        {
            Console.WriteLine("date is 6 months later then birthday");
        }
        else
        {
            Console.WriteLine("wait for it");
        }
Gelootn
  • 591
  • 5
  • 16
0

You could calculte the difference between dates using Subtract method and calculate how many months you have between these dates, for sample:

DateTime birthDay = /* some date */;
DateTime someDate = /* some date */;

var months = someDate.Subtract(birthDay).Days / (365.25 / 12);

This answer provides a good helper for Dates: https://stackoverflow.com/a/33287670/316799

Community
  • 1
  • 1
Felipe Oriani
  • 36,796
  • 18
  • 129
  • 183