19

I need to compare dates in two separate list. Each list is constructed of MyFile Objects. That is a class that I created in order to have specific information about a file such as name, dateModified, extension, etc. The only problem is that a lot of MyFiles objects in my second list (got those from external hard drive) do not have the dateTime stamp (LastWriteTime) till the millisecond. I believe that is the reason why my comparison is not working. For example here is an example of how my comparison is failing: "Why does c# thinks the dates are not equal?" Debug

a and b are MyFile objects and MyFile class contains a property ticks and that is equal to the file.LastWriteTime.Ticks they are not used in the program I just included them for debugging purposes. So after debugging several times I realized that the last 7 digits represent the milliseconds of a file. As a result my ticks property in MyFile now contains 11 significant figures instead than 18 ( note 18-11 = 7). The problem with this is that when I compare the ticks I get strange results when I try to update the ticks property by dividing by 10000000 and then multyplying by 10000000. Since my ticks propery is a long int it will lose the last 7 digits when I divide. I get less 'errors'. But there is some other times when I get something like this: enter image description here

Here we can see that the dates are the same at least till the second. Why is c# thinking its not the same date? Do I have to create my own "Ticks" function? I know I convert dateTime to string then compare it but I want to have the posiblility of knowing if a object a.dateModified is newer than object b.dateModified

Tono Nam
  • 34,064
  • 78
  • 298
  • 470
  • Do the two dates actually have different `Millisecond` values? – Oded May 20 '11 at 15:50
  • Here is a working method: http://stackoverflow.com/questions/1004698/how-to-truncate-milliseconds-off-of-a-net-datetime Check the answer by viggity – Jerry Mar 05 '13 at 19:28

1 Answers1

35

Try comparing with specific precision:

DateTime a, b;
// fill a and b with the values you need
if (Math.Abs((a-b).TotalSeconds) < 1)
    Console.WriteLine("File doesn't need to be copied");
else
    Console.WriteLine("File needs to be copied");
Zruty
  • 8,377
  • 1
  • 25
  • 31
  • You need to truncate. `TotalSeconds` can be `0.3` – SLaks May 20 '11 at 15:52
  • Yes, thanks. I looked it up just after posting. Also `TimeSpan` can be negative, so I included `Math.Abs()` – Zruty May 20 '11 at 15:53
  • Edit your answer to: ------> if ((a-b).TotalSeconds < 1) because the TotalSeconds property returns a double and I got a result such as 0.4512..... I will accept your answer – Tono Nam May 20 '11 at 16:00
  • Well, you need to decide your precision yourself. If the error margin is as high as 1 second, make a threshold equal to 1 second. I edited the answer. – Zruty May 20 '11 at 16:03
  • Note that TotalSeconds is present on TimeSpan. So, the above would work, or you could use TimeOfDay to get a TimeSpan from DateTime. – ryanwebjackson Nov 09 '19 at 16:38