DateTime Comparison Problems

  • Thread starter Thread starter elziko
  • Start date Start date
E

elziko

Hi I have two date times that I belive to be equal. However, when I do a
comparison I find that date1 is less than date2...

Console.WriteLine(date1.ToString("dd/MM/yyyy HH:mm:fffffff") + " - " +
date2.ToString("dd/MM/yyyy HH:mm:fffffff"))

.... which ouputs: 12/11/2003 14:23:4370000 - 12/11/2003 14:23:4370000 in the
console window.... this is as much info as DateTime can handle right? So we
know that they are equal?? Then I try the actual comparison...

Console.WriteLine(DateTime.Compare(date1, date2))

....which ouputs -1. Although it should output 0. Whats going on here?
 
elziko said:
Hi I have two date times that I belive to be equal. However, when I
do a comparison I find that date1 is less than date2...

Console.WriteLine(date1.ToString("dd/MM/yyyy HH:mm:fffffff") + "
- " +
date2.ToString("dd/MM/yyyy HH:mm:fffffff"))

... which ouputs: 12/11/2003 14:23:4370000 - 12/11/2003 14:23:4370000
in the console window.... this is as much info as DateTime can handle
right? So we know that they are equal?? Then I try the actual
comparison...

Console.WriteLine(DateTime.Compare(date1, date2))

...which ouputs -1. Although it should output 0. Whats going on
here?


Why not output the seconds?

date1.ToString("dd/MM/yyyy HH:mm:ss.fffffff")
 
dates can sometimes do strange things :/

1 way around this is to convert them to strings and compare them ( i dont
know if you are lookiung for something like this but just in case ;))

'dont use the :fffffff if you dont really need them
if date1.ToString("dd/MM/yyyy HH:mm:fffffff") = date2.ToString("dd/MM/yyyy
HH:mm:fffffff") then
Console.WriteLine("0")
else
Console.WriteLine("-1")
end if


hope it helps

eric
 
1 way around this is to convert them to strings and compare them

I will eventually need to know that if the dates are not equal then which
one precedes the other? There must be a way of doing this without resorting
to creating strings and writing my own comparison code... thats pretty
inefficient especially when you need to do a lot of them I'd imagine.

I may look into converting them to ticks and comparing them... maybe this
would be more efficient that strings.

Thanks anyway though!
 
Well Iwent the ticks route and found the source of my problems... the way in
which I was storing and retrieving my data meant that the two dates were
always a few ticks different for some reason.... not sure why so now I'm
subtracting one from the other and ignoring differences of less than a
second.
 
Back
Top