0

I have to compare two datetime objects.

I have dt1= 08/02/2017 11:37:06 and dt2=08/02/2017 11:37:06 224. So for sure they do not work in the condition

if(dt1>=dt2) {}

and

if(DateTime.Equals(dt1, dt2)) { }

Both of them return false. How to make them work ? I mean it should go inside the loop (avoiding the 224 part of datetime). I just have to take into account dd/mm/yyy hh:mm:ss and eleminate other things

  • 3
    Surely you want `if (dt1.Date >= dt2.Date) {}`? Or do you want time without milliseconds? – Equalsk Feb 08 '17 at 10:48
  • Maybe [related](http://stackoverflow.com/questions/1004698/how-to-truncate-milliseconds-off-of-a-net-datetime) – Pikoh Feb 08 '17 at 10:49
  • One option would be to check the magnitude of the difference between the dates and compare it to a minimum allowed epsilon, but really we would need to know more specifics to determine exactly what would make sense for your specific case. – juharr Feb 08 '17 at 10:51
  • Using Date() truncates the DataTime to midnight. You can get the hours/minutes/seconds by subtracting the DateTime object from the midnight time. A DateTime is a double with the Integer portion of the time being the number of days from 1/1/1900 and the fraction being a part of a day like .25 being 6 hours (6 hours /24 hours). – jdweng Feb 08 '17 at 10:52
  • @Equalsk Yes it should go in the loop. I mean without millisecond. – tosttest tosttest Feb 08 '17 at 10:55
  • @jdweng You can just use the `TimeOfDay` property of `DateTime` to get the time portion. – juharr Feb 08 '17 at 10:55
  • @Equalsk So you just want to truncate the milliseconds or would you want something like "11:37:06.999" to be equivalent to "11:37:07.001"? – juharr Feb 08 '17 at 10:58
  • I just have to take into account dd/mm/yyy hh:mm:ss and eleminate other things . – tosttest tosttest Feb 08 '17 at 11:04

0 Answers0