user745607
user745607

Reputation: 707

How to Compare DateTime C# Months and weeks

I need to compare a date in C# if the date is less than 12 months,i need to set a boolean value

My Code is

    String d  = "26/06/10";
    DateTime dt = DateTime.ParseExact(d, "dd/MM/yy", null);
    if ((dt > DateTime.Now.AddMonths(-12)  ) )
    {
        Console.WriteLine("It is less than 12 months");
    }
    else
    {
        Console.WriteLine("It is more than 12 months");
    }

Is the best way to compare date in c#. similarly i need to compare date is less than two weeks or not

Any help appreciated

Thanks

sup

Upvotes: 1

Views: 8924

Answers (5)

Bibhu
Bibhu

Reputation: 4081

DateTime date1 = DateTime.Now.AddMonths(-12)
if(DateTime.Compare(dt, date1 )
{
//provided date is within 12 months
}
else
{
//provided date is after 12 months
}

Upvotes: 0

Sascha Hennig
Sascha Hennig

Reputation: 2572

For a clearer understanding: you do not want to compare two dates (or DateTimes) but two TimeSpans. Namely the difference in time between now and the date you supplied - and the a time span of 12 months.

String d = "26/06/10";
DateTime dt = DateTime.ParseExact(d, "dd/MM/yy", CultureInfo.InvariantCulture);
TimeSpan deltaTimeSpan = dt - DateTime.Now;      // get the time difference between now and the time given
TimeSpan twelveMonths = new TimeSpan(365,0,0,0); // get a time span of 12 months

// round the amount of days down and always supply a positive number of days
int deltaTime = Convert.ToInt32(Math.Abs(Math.Floor(deltaTimeSpan.TotalDays)));

if (twelveMonths.TotalDays > deltaTime)
{
    Console.WriteLine(string.Format("It is less than 12 months ({0} days).", deltaTime));
}
else if (twelveMonths.TotalDays < deltaTime)
{
    Console.WriteLine(string.Format("It is more than 12 months ({0} days).", deltaTime));
}
else
{
    Console.WriteLine(string.Format("The difference in time is exactly 12 months. ({0} days).", deltaTime);
}

Take note that this example certainly does not take in account leap years. The code does take in account weather the year to compare with lies in the past or the future (by converting the TimeSpan into a positive value and comparing against that one).

Adjusting the above code to do the same for two weeks or any other time span should be simple enough. Just change the TimeSpan I named "twelveMonths".

Upvotes: 0

Vamsi
Vamsi

Reputation: 4253

You could use TimeSpan to get the difference between two DateTime values

String d = "26/06/10";
DateTime dt = DateTime.ParseExact(d, "dd/MM/yy", null);
DateTime dt2 = DateTime.Now.AddMonths(-12);

TimeSpan ts = dt - dt2;

You can use ts.Days to compare

Upvotes: 4

Kamyar
Kamyar

Reputation: 18797

For two weeks:

if (dt1.Subtract(dt2).Days > 14)
{
    ...
}  

For 12 Months(one year) (Considering day of the month is not important):

var monthDifference = ((dt1.Year - dt2.Year) * 12) + dt1.Month - dt2.Month

Upvotes: 0

FIre Panda
FIre Panda

Reputation: 6637

You could do

DateTime date2 = DateTime.Now.AddMonths(-12);
  //Or if you want to neglect the time part you could do
DateTime date2 = new DateTime(DateTime.Now.Year,DateTime.Now.Month,DateTime.Now.Day,0,0,0).AddMonths(-12);
String d = "26/06/10"; 
DateTime date1 = DateTime.ParseExact(d, "dd/MM/yy", null);
int result = DateTime.Compare(date1, date2);
string res;

if (result < 0)
   Console.WriteLine("It is less than 12 months"); 
else if (result == 0)
   res = "is the equal";         
else
    Console.WriteLine("It is more than 12 months"); 

The problem with your code snippet is that it will output "It is more than 12 months" even if the date is equal.

Upvotes: 0

Related Questions