.Net Date Compare
-
Hi Guys, I've got some dates i'm storing in a database and XML file, and when i read them out and compare them to the origional date that remained in code, they return false due to a loss in precision. Is there any easy way to compare dates with a certain precision comparison? Or trim the precision on dates in code? The ones i have go all the way down to the milliseconds. Cheers Tris
------------------------------- Carrier Bags - 21st Century Tumbleweed.
-
Hi Guys, I've got some dates i'm storing in a database and XML file, and when i read them out and compare them to the origional date that remained in code, they return false due to a loss in precision. Is there any easy way to compare dates with a certain precision comparison? Or trim the precision on dates in code? The ones i have go all the way down to the milliseconds. Cheers Tris
------------------------------- Carrier Bags - 21st Century Tumbleweed.
Hi,
DateTime.Date
sets you back to midnight, which is fine for a timeless date compare. :)Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
-
Hi,
DateTime.Date
sets you back to midnight, which is fine for a timeless date compare. :)Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
ya I have tried it. Its perfect DateTime d1 = DateTime.Today; DateTime d2 = DateTime.Today.AddMilliseconds(1); if (d1.Date.CompareTo(d2.Date) == 0) { Response.Write("Ya its equal"); } else { Response.Write("No its not equal"); }
Education is not a way to escape poverty — it is a way of fighting it.
-
Hi,
DateTime.Date
sets you back to midnight, which is fine for a timeless date compare. :)Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
Thanks Luc, is there any way to do date and time, without the MS? It's just possible that there might be multiple values for the same day, so that's not really fine grain enough for me!
------------------------------- Carrier Bags - 21st Century Tumbleweed.
-
Thanks Luc, is there any way to do date and time, without the MS? It's just possible that there might be multiple values for the same day, so that's not really fine grain enough for me!
------------------------------- Carrier Bags - 21st Century Tumbleweed.
Hi, it all depends on what you want exactly. 1. if parts of the DateTime should be identical, then check those parts, as in:
DateTime dt1=...;
DateTime dt2=...;
if (dt1.Date==dt2.Date && dt1.Hours==dt2.Hours) isSufficientlyEqual();the problem now is that 16:59:59 would equal to 16:00:00 and not to 17:00:00 which is much closer! 2. if time distance is important, calculate it
DateTime dt1=...;
DateTime dt2=...;
if (dt1.Date==dt2.Date) {
float seconds=dt1.Subtract(dt2).TotalSeconds;
if (seconds>-100 && seconds<100) isSufficientlyClose();
}3. Up to the second equal:
DateTime dt1=...;
DateTime dt2=...;
if (dt1.AddMilliseconds(-dt1.MilliSeconds)==dt2.AddMilliseconds(-dt2.MilliSeconds)) {
isPrettyClose();
}:)
Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
-
Hi, it all depends on what you want exactly. 1. if parts of the DateTime should be identical, then check those parts, as in:
DateTime dt1=...;
DateTime dt2=...;
if (dt1.Date==dt2.Date && dt1.Hours==dt2.Hours) isSufficientlyEqual();the problem now is that 16:59:59 would equal to 16:00:00 and not to 17:00:00 which is much closer! 2. if time distance is important, calculate it
DateTime dt1=...;
DateTime dt2=...;
if (dt1.Date==dt2.Date) {
float seconds=dt1.Subtract(dt2).TotalSeconds;
if (seconds>-100 && seconds<100) isSufficientlyClose();
}3. Up to the second equal:
DateTime dt1=...;
DateTime dt2=...;
if (dt1.AddMilliseconds(-dt1.MilliSeconds)==dt2.AddMilliseconds(-dt2.MilliSeconds)) {
isPrettyClose();
}:)
Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
Once you have arrived at the desired formater, create an extension method(s) for the Date Time class. This will clean up any code complexities introduced. Some Formating and Parsing Tips[^] Some DateTime Extension Method Examples[^]
-
Hi Guys, I've got some dates i'm storing in a database and XML file, and when i read them out and compare them to the origional date that remained in code, they return false due to a loss in precision. Is there any easy way to compare dates with a certain precision comparison? Or trim the precision on dates in code? The ones i have go all the way down to the milliseconds. Cheers Tris
------------------------------- Carrier Bags - 21st Century Tumbleweed.
Depending on which database you are using, you may see some loss of precision when you store a datetime (aka timestamp in some databases). SQL Server, for example, does not store datetime values to the exact millisecond. It is only accurate to approximately the nearest 3 milliseconds. So if it is important to get accurate timestamps right down to the exact millisecond, you should avoid the SQL Server datetime datatype. SQL Server 2008 introduces a new datatype, datetime2, which is accurate to approximately 100ns to get around this problem. Possible solutions: - if you are using SQL Server 2008 and have ownership of the database, use datetime2 rather than datetime - if you are not using SQL Server 2008 or you cannot change the table design and if you do not need millisecond accuracy, decide what accuracy you do need: for example, round off your timestamps to the nearest second before storing them - if you do need millisecond accuracy, either convert the timestamp to a string format (OK for XML, not good for date functions and the conversion to and from datetime type may be slow) or store the timestamp as the number of milliseconds from the epoch (gives you the accuracy you need at a reasonable performance but not good for date functions)
-
Once you have arrived at the desired formater, create an extension method(s) for the Date Time class. This will clean up any code complexities introduced. Some Formating and Parsing Tips[^] Some DateTime Extension Method Examples[^]
That's a nice idea! Something to stick in the Common Lib. ^^
------------------------------- Carrier Bags - 21st Century Tumbleweed.
-
Hi, it all depends on what you want exactly. 1. if parts of the DateTime should be identical, then check those parts, as in:
DateTime dt1=...;
DateTime dt2=...;
if (dt1.Date==dt2.Date && dt1.Hours==dt2.Hours) isSufficientlyEqual();the problem now is that 16:59:59 would equal to 16:00:00 and not to 17:00:00 which is much closer! 2. if time distance is important, calculate it
DateTime dt1=...;
DateTime dt2=...;
if (dt1.Date==dt2.Date) {
float seconds=dt1.Subtract(dt2).TotalSeconds;
if (seconds>-100 && seconds<100) isSufficientlyClose();
}3. Up to the second equal:
DateTime dt1=...;
DateTime dt2=...;
if (dt1.AddMilliseconds(-dt1.MilliSeconds)==dt2.AddMilliseconds(-dt2.MilliSeconds)) {
isPrettyClose();
}:)
Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
Ah, i was wondering if there was a existing or simpler way. I could have done it by now >< I think between that and some extention methods, i can solve the problem nicely. Cheers!
------------------------------- Carrier Bags - 21st Century Tumbleweed.
-
Depending on which database you are using, you may see some loss of precision when you store a datetime (aka timestamp in some databases). SQL Server, for example, does not store datetime values to the exact millisecond. It is only accurate to approximately the nearest 3 milliseconds. So if it is important to get accurate timestamps right down to the exact millisecond, you should avoid the SQL Server datetime datatype. SQL Server 2008 introduces a new datatype, datetime2, which is accurate to approximately 100ns to get around this problem. Possible solutions: - if you are using SQL Server 2008 and have ownership of the database, use datetime2 rather than datetime - if you are not using SQL Server 2008 or you cannot change the table design and if you do not need millisecond accuracy, decide what accuracy you do need: for example, round off your timestamps to the nearest second before storing them - if you do need millisecond accuracy, either convert the timestamp to a string format (OK for XML, not good for date functions and the conversion to and from datetime type may be slow) or store the timestamp as the number of milliseconds from the epoch (gives you the accuracy you need at a reasonable performance but not good for date functions)
I'm not too fussed about the loss of precision, i can deal with the nearest second. But thanks for the input. Is there any easy way to round a date? I've always created new dates and copied the relevant values over before.
------------------------------- Carrier Bags - 21st Century Tumbleweed.
-
I'm not too fussed about the loss of precision, i can deal with the nearest second. But thanks for the input. Is there any easy way to round a date? I've always created new dates and copied the relevant values over before.
------------------------------- Carrier Bags - 21st Century Tumbleweed.
One of the earlier replies from Luc shows you how to do this. Yes, you have to create a new DateTime (it is immutable so once you've created one you can't change it) but there is an easy way to do it using AddMilliseconds. Sounds like a good candidate for an extension method if you're at the right version of .NET. Otherwise, a good old fashioned DateUtils class would do.
-
Hi Guys, I've got some dates i'm storing in a database and XML file, and when i read them out and compare them to the origional date that remained in code, they return false due to a loss in precision. Is there any easy way to compare dates with a certain precision comparison? Or trim the precision on dates in code? The ones i have go all the way down to the milliseconds. Cheers Tris
------------------------------- Carrier Bags - 21st Century Tumbleweed.
May I humbly recommend DateTruncate[^] ?
-
May I humbly recommend DateTruncate[^] ?
PIEBALDconsult wrote:
May I humbly recommend DateTruncate?
Waiting for V2.0, with a Compare method added to it. :-D
Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
-
PIEBALDconsult wrote:
May I humbly recommend DateTruncate?
Waiting for V2.0, with a Compare method added to it. :-D
Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
You'll be waiting a long time if you don't make a request. I suppose I could make a TimespanTruncate.