0

I have two datetimes which I do want to compare.

Currently, this is the code im using:

if (entry.Year == time.Year && 
    entry.Month == time.Month &&
    entry.Day == time.Day &&
    entry.Hour == time.Hour &&
    entry.Minute == time.Minute)
{ 
   // condition met!
}

To me, such a big chunk of code is quite hard to grasp at first glance when scrolling over. Is there a more clean way to do that or would the best option be to create a small function for this?

Important: I want to check if the datetimes are both in the same minute and not if there are max 60 seconds apart.
So for example:

  • 2022/10/10 10:10:01 matches 2022/10/10 10:10:59
  • 2022/10/10 10:10:59 does not match 2022/10/10 10:11:01
julian bechtold
  • 1,875
  • 2
  • 19
  • 49
  • Code refactoring suggestions are generally opinion based. If you don't like comparison written that way I picked duplicate that shows how to remove seconds in shorter code... but really rewrite it in any way you find readable - there is no direct way to compare just minutes (note that you may need to compare Kind too if you care for correctness). – Alexei Levenkov Oct 10 '22 at 20:09

0 Answers0