8

This is may be silly question. But I am missing logic here. I have to compare dates with date time with hours and minutes (not with seconds).

IF first field time is older then second field execute condition

right now I am doing if (Convert.ToDateTime(newItem["Modified"]) < Convert.ToDateTime(properties.ListItem["Modified"]))

example if("02/12/2015 11:58" < "02/12/2015 12:01") then execute condition.

James123
  • 11,184
  • 66
  • 189
  • 343

5 Answers5

6

You could create new DateTime objects with mostly the same values, but with seconds set to 0. Example:

DateTime date1WithoutSeconds = new DateTime(dt1.Year, dt1.Month, dt1.Day, dt1.Hour, dt1.Minute, 0);
DateTime date2WithoutSeconds = new DateTime(dt2.Year, dt2.Month, dt2.Day, dt2.Hour, dt2.Minute, 0);

bool b = date1WithoutSeconds < date2WithoutSeconds;
MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
ama1111
  • 569
  • 3
  • 10
0

You could subtract the two dates, and if the TotalSeconds of the difference is less than 60 AND the minues are the same, then they are equal:

var first = Convert.ToDateTime(newItem["Modified"]);
var second = Convert.ToDateTime(properties.ListItem["Modified"]);

if (first.Subtract(second).TotalSeconds < 60 && first.Minute == second.Minute)
{
    Console.WriteLine("They are equal");
}
Rufus L
  • 36,127
  • 5
  • 30
  • 43
0

One more way that should work.

DateTime date1 = Convert.ToDateTime(newItem["Modified"]);
DateTime date2 = Convert.ToDateTime(properties.ListItem["Modified"]));

if( date1.AddSeconds(-date1.Second) < date2.AddSeconds(-date2.Second) ) {

}

But, I would wonder...is it really that you need to ignore the seconds and "floor" the result so that 12:59:00 is the same as 12:59:59 but different than 12:58:59 even though there's only a second of difference...or do you need to know that it's greater than a minute of difference? If you really just want to make sure that it is a minute apart, use TimeSpan (date1 - date2).TotalSeconds > 60

I doubt this is likely, but if your DateTime is a string WITH milliseconds, then do:

if( date1.AddSeconds(-date1.Second).AddMilliseconds(-date1.Millisecond) <
    date2.AddSeconds(-date2.Second).AddMilliseconds(-date2.Millisecond) ) 
{

}
Kevin Nelson
  • 7,613
  • 4
  • 31
  • 42
  • I was also wondering that. But I interpreted the question as 'ignore seconds', so that `1:00:59 < 1:01:01` – Rufus L Feb 12 '15 at 18:27
  • @RufusL, yeah, sometimes people aren't sure what they really need, so I like to throw out extra crap and see what happens :-P – Kevin Nelson Feb 12 '15 at 18:28
  • Really should truncate the milliseconds as well, just in case. – juharr Feb 12 '15 at 18:54
  • @juharr, since both appear to be coming from strings, I think it's very unlikely that the strings have milliseconds...but I went ahead and added it. – Kevin Nelson Feb 12 '15 at 20:40
0

You should use the DateTime.CompareTo method.

Grab and assign both dates as DateTime objects:

DateTime date = Convert.ToDateTime(newItem["Modified"]);
DateTime compareDate = Convert.ToDateTime(properties.ListItem["Modified"]);

You can now use the CompareTo method of the DateTime object to see if the instance is earlier, the same, or later than the other, returning -1, 0, and 1 respectively.

So, following your example: if("02/12/2015 11:58" < "02/12/2015 12:01"), first date being date and second being compareDate, the code:

date.CompareTo(compareDate);

will return -1, telling you the instance invoking the method is earlier than the object you are comparing it to.

Here is the MSDN.

Dan Orlovsky
  • 1,045
  • 7
  • 18
-1

First of all, the sample data you've mentioned in your question doesn't include seconds, so by default Convert.ToDateTime will assign '00' as seconds, so it would compare without the seconds. But let's say that you do provide seconds in the actual data. You can use the following:

var date1 = Convert.ToDateTime(newItem["Modified"]);
var date2 = Convert.ToDateTime(properties.ListItem["Modified"]);
if (date1.AddSeconds(-date1.Second) < date2.AddSeconds(-date2.Second))
DoronG
  • 2,576
  • 16
  • 22
  • 1
    `date1.AddSeconds(-date1.Second)` - and what if the dates have milliseconds? – CodeCaster Feb 12 '15 at 18:29
  • 1
    Not part of the OP's requirements – DoronG Feb 12 '15 at 22:55
  • We're not here to fulfill requirement, we're here to help people build great software components. By not thinking ahead of what OP is asking, you're not doing anyone a favor. – CodeCaster Feb 13 '15 at 06:30
  • 1
    @CodeCaster: With all due respect. Great software should solve a real-life problem. i agree some people create awesome software for research and for just the fun of it, but most software is created for the sake of answering a need. In this case, though there could be several edge-case scenarios, like you've mentioned above, the OP may no care on the impact of msecs and/or the data may never have msecs (see sample data provided by OP). In any case, I'll take the vote-down as a lesson learned – DoronG Feb 13 '15 at 14:57
  • My point is that the web in general and this site in specific is already full of code snippets that serve a very specific purpose, ignoring any edge cases or being incomplete simply because the asker didn't realize they were forgetting a detail. Working with dates isn't trivial, and I have linked four Q&As that do exactly what OP wants, where those edge cases and workarounds are discussed. Please don't take offense, but I don't think your code does what OP wants, and you're only partially to blame because OP didn't ask a complete question. – CodeCaster Feb 13 '15 at 15:03