3

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

Short question! :-)

janhartmann
  • 14,713
  • 15
  • 82
  • 138
  • 1
    @OP: do you mean "within a year of each other" or "in the same year?" – belgariontheking May 13 '09 at 20:44
  • 1
    Please be more specific about what exact you want. You could either have the two days be no more than 365 days apart or have both dates in the same year (simple case) – schnaader May 13 '09 at 20:46
  • 5
    I thought this was a glory post from a programmer who had dates with two different women in the same calendar year for the first time =/ – sclarson May 13 '09 at 21:16

8 Answers8

20
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
  • 399,467
  • 113
  • 570
  • 794
  • 1
    What if I'd like to compare 1st Jan 2008 and 31st Dec 2008? This expression would be telling me a lie! – pestaa May 13 '09 at 20:48
  • What do you recommend then, pestaa? – janhartmann May 13 '09 at 20:49
  • @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 May 13 '09 at 20:50
  • 1
    Magic number warning! What about leap year? :) – patjbs May 13 '09 at 20:51
  • Taking leap years into consideration. – pestaa May 13 '09 at 20:51
  • I suggest to simply check whether year % 4 == 0. If so, abs(totaldays) < 366, otherwise it remains 365. – pestaa May 13 '09 at 20:53
  • @pestaa: that's not good enough either. 1900, and 2100 would both fail that. See my update. – Joel Coehoorn May 13 '09 at 20:54
  • 4
    @pestaa: you could get round that case by: if (d1 < d2 ? d2 < d1.AddYears(1) : d1 < d2.AddYears(1)) – JDunkerley May 13 '09 at 20:54
  • 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 May 13 '09 at 20:58
  • 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 May 13 '09 at 20:59
  • 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 May 13 '09 at 21:03
  • Hey, Joel, this gets kinda confusing. Once you sacrifice readability to achieve flexibility, now you ignore equality? Give it some thought before. – pestaa May 13 '09 at 21:03
  • @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 May 13 '09 at 21:09
2

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 );
Ricardo Felgueiras
  • 3,589
  • 6
  • 25
  • 27
0
TimeSpan ts = Date1.Subtract(Date2);

if(ts.Days > 365)
{
// Over a year.
}
Serapth
  • 7,122
  • 4
  • 31
  • 39
0

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
  • 4,749
  • 2
  • 23
  • 32
0

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
  • 12,106
  • 12
  • 71
  • 90
0

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;
}
Community
  • 1
  • 1
Wedge
  • 19,513
  • 7
  • 48
  • 71
0

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
  • 8,002
  • 1
  • 34
  • 48
-2
if (year(date1) == year(date2))
{
   //true
}
else
{
   //false
}
TheTXI
  • 37,429
  • 10
  • 86
  • 110
  • 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 May 13 '09 at 20:44
  • Sorry if I misguided my question. Joel's answer did the trick. Thanks for your effort. – janhartmann May 13 '09 at 20:47
  • 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 May 13 '09 at 20:50