Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare a given date from today

Tags:

c#

datetime

I want to compare a given date to today and here is the condition: If provided date is greater than or equal to 6 months earlier from today, return true else return false

Code:

string strDate = tbDate.Text; //2015-03-29
if (DateTime.Now.AddMonths(-6) == DateTime.Parse(strDate)) //if given date is equal to exactly 6 months past from today (change == to > if date has to be less 6 months)
{
    lblResult.Text = "true"; //this doesn't work with the entered date above.
}
else //otherwise give me the date which will be 6 months from a given date.
{
    DateTime dt2 = Convert.ToDateTime(strDate);
    lblResult.Text = "6 Months from given date is: " + dt2.AddMonths(6); //this works fine
}
  • If 6 months or greater than 6 months is what I would like for one condition
  • If less than 6 months is another condition.
like image 387
Si8 Avatar asked Sep 29 '15 16:09

Si8


People also ask

How to compare dates in Excel?

Example #1 1 Look at the below data to compare dates in excel. 2 Now in cell C2, apply the formula as “=A2=B2”. 3 Press Enter to get the result.#N#We have the same date in both columns, so we got TRUE as a result. 4 Drag the formula to the following columns as well to get the result. More ...

How to compare results with today’s date in SQL?

In this article, we will see SQL Query to Compare results with Today’s date by comparing the data with today’s date using GETDATE () function of SQL. GETDATE () function: This function is used to return the present date and time of the database system. It returns the current date and time of the system.

Which one to use to calculate date difference in days?

Which one to use depends on exactly what your needs are. Example 1. Excel DATEDIF formula to calculate date difference in days Supposing you have the start date in cell A2 and the end date in cell B2 and you want Excel to return the date difference in days. A simple DATEDIF formula works just fine:

Which method is used to compare two dates?

For comparing the two dates, we have used the compareTo () method. If both dates are equal it prints Both dates are equal. If date1 is greater than date2, it prints Date 1 comes after Date 2. If date1 is smaller than date2, it prints Date 1 comes after Date 2.


2 Answers

Your first problem is that you're using DateTime.Now instead of DateTime.Today - so subtracting 6 months will give you another DateTime with a particular time of day, which is very unlikely to be exactly the date/time you've parsed. For the rest of this post, I'm assuming that the value you parse is really a date, so you end up with a DateTime with a time-of-day of midnight. (Of course, in my very biased view, it would be better to use a library which supports "date" as a first class concept...)

The next problem is that you are assuming that subtracting 6 months from today and comparing it with a fixed date is equivalent to adding 6 months to the fixed date and comparing it with today. They're not the same operation - calendar arithmetic just doesn't work like that. You should work out which way you want it to work, and be consistent. For example:

DateTime start = DateTime.Parse(tbDate.Text);
DateTime end = start.AddMonths(6);
DateTime today = DateTime.Today;
if (end >= today)
{
    // Today is 6 months or more from the start date
}
else
{
    // ...
}

Or alternatively - and not equivalently:

DateTime target = DateTime.Parse(tbDate.Text);
DateTime today = DateTime.Today;
DateTime sixMonthsAgo = today.AddMonths(-6);
if (sixMonthsAgo >= target)
{
    // Six months ago today was the target date or later
}
else
{
    // ...
}

Note that you should only evaluate DateTime.Today (or DateTime.Now etc) once per set of calculations - otherwise you could find it changes between evaluations.

like image 86
Jon Skeet Avatar answered Oct 10 '22 08:10

Jon Skeet


Try with this

DateTime s = Convert.ToDateTime(tbDate.Text);
s = s.Date;

if (DateTime.Today.AddMonths(-6) == s) //if given date is equal to exactly 6 months past from today (change == to > if date has to be less 6 months)
{
lblResult.Text = "true"; //this doesn't work with the entered date above.
}

replace == with >= or <= according to your needs

like image 30
CDrosos Avatar answered Oct 10 '22 09:10

CDrosos