When CompareTo() of DateTime in .NET is called, is it comparing the ticks on a DateTime object to the ticks on another one? How precise is the comparison? I’m fetching datetime2(7) values from a sql database and want to make sure the comparison is ticks-wise.
Share
Yes, ticks are compared. This is how
Equalsimplemented in DateTime structureInternalTicks returns field of
ulongtype. So, comparison of DateTimes have same precision as comparison of long type. Single tick represents 100 nanoseconds.Btw database can store time with different precision – some accuracy could be lost. But there is a trick – you can save
Ticksvalue to database. In this case you can restore exactly same .net DateTime object later.