views:

337

answers:

9

I got two dates, how do I check if both dates does not exceed over one year?

Short question! :-)

+15  A: 
if (Math.Abs((d1 - d2).TotalDays) < 365)


Edit: This should account for leap years better.

if ( d1 <= d2 && d1.AddYears(1) >= d2 || d2 < d1 && d2.AddYears(1) > d1)

Update:
I like @JDunkerley's solution better:

if (d1 < d2 ? d2 < d1.AddYears(1) : d1 < d2.AddYears(1))
Joel Coehoorn
This did the trick. Thanks :-)
meep
What if I'd like to compare 1st Jan 2008 and 31st Dec 2008? This expression would be telling me a lie!
pestaa
What do you recommend then, pestaa?
meep
@pestaa: This is true. It's naive regarding leap years. I originally used .TotalYears and then realized TimeSpan doesn't have that. I'll have something more complete up in a few moments.
Joel Coehoorn
Magic number warning! What about leap year? :)
patjbs
Taking leap years into consideration.
pestaa
I suggest to simply check whether year % 4 == 0. If so, abs(totaldays) < 366, otherwise it remains 365.
pestaa
@pestaa: that's not good enough either. 1900, and 2100 would both fail that. See my update.
Joel Coehoorn
@pestaa: you could get round that case by:if (d1 < d2 ? d2 < d1.AddYears(1) : d1 < d2.AddYears(1))
JDunkerley
Is that simple in C#? Thank god there are competent programmers. :) I'm not familiar with this language. Though, this was a simple yet exciting discussion.
pestaa
No offense to the original poster, but isn't this answer somewhat flawed by the fact it re-invented the wheel of something that is already implemented in the framework ( System.TimeSpan ) with a roll your own solution, which as it stood initially contained a bug? Nothing to mention readability.
Serapth
The original solution used a TimeSpan. I agree it's flawed as a result of a mistake admitted earlier, hence the hastily implemented edit that should now look a little better.
Joel Coehoorn
Hey, Joel, this gets kinda confusing. Once you sacrifice readability to achieve flexibility, now you ignore equality? Give it some thought before.
pestaa
@pestaa: I don't have an IDE handy and typed the initial answer directly into the browser, expecting there to be a .TotalYears property on timespan. I immediately realized that mistake, and spend several updates correcting it one bit at a time. What I came up with isn't as nice as JDunkerley's, but I didn't want to just copy.
Joel Coehoorn
It works like a charm. :-)
meep
A: 
if (year(date1) == year(date2))
{
   //true
}
else
{
   //false
}
TheTXI
This would answer the question as it is posed in the question title. If you are looking for something that will make sure two dates are within a year span of time, then Joel's or another answer would be more suitable. This just checks if the year is the same.
TheTXI
Sorry if I misguided my question. Joel's answer did the trick. Thanks for your effort.
meep
meep: It's alright. Although the slew of downvotes on it AFTER I explicitly stated that another may be more useful is pretty shady. Oh well.
TheTXI
No down-vote from here.
meep
+1  A: 

I give you a little example:

DateTime startTime = DateTime.Now;

 DateTime endTime = DateTime.Now.AddSeconds( 75 );

 TimeSpan span = endTime.Subtract ( startTime );
 Console.WriteLine( "Time Difference (seconds): " + span.Seconds );
 Console.WriteLine( "Time Difference (minutes): " + span.Minutes );
 Console.WriteLine( "Time Difference (hours): " + span.Hours );
 Console.WriteLine( "Time Difference (days): " + span.Days );
arpf
A: 
TimeSpan ts = Date1.Subtract(Date2);

if(ts.Days > 365)
{
// Over a year.
}
Serapth
I'm not sure this handles leap years correctly either.
pestaa
A: 

That depends on the date format.

  • If you have two timestamps, you may calculate the difference between them.
  • If you have two specific dates in a known format, you simply compare the year attributes as strings.
pestaa
A: 

If they're both in DateTime structures, then you can just subtract the two to get a Timespan structure. The Timespan structure has a Days property which you can look at.

So you'll have something like:

if(Math.Abs((date1 - date2).Days) <= 365) ...
ajma
A: 

This is the same question as "how do I calculate someone's age".

Blatantly stealing the answer from there and modifying it for use:

public static bool DatesAreWithinOneYear(DateTime date1, DateTime date2)
{
    DateTime startDate = date2 > date1 ? date1 : date2;
    DateTime endDate = date2 > date1 ? date2 : date1;

    int years = endDate.Year - startDate.Year;
    if (endDate < startDate.AddYears(years))
    {
        years--;
    }
    return years < 1;
}
Wedge
A: 

If necessary, swap t1 and t2 so that t1 <= t2

if(t1.AddYears(1) >= t2) {
    // t1 is within a year of t2
    return true;
} else {
    // t1 is not within a year of t2
    return false;
}
Aric TenEyck
A: 

Takes into account leap years, etc.

if (    Math.Abs((d1 - d2).TotalDays)
     <= Math.Abs((new DateTime(d1.Year, 1, 1)
                  - new DateTime(d2.Year, 1, 1)).TotalDays))
{
   // Within 1 year
}
sixlettervariables