Comparing Dates

M

matthias s.

Hi there,

I'm surely not the first one to have this problem. But searching the group
didn't yield any results, so here goes:

When storing DateTime values in an SQL Database (using a 'datetime' field),
the value returned by a subsequent query never exactly matches the DateTime
values inserted (I guess the DateTime struct has a different precision then
what is acutally saved to the DB), thus the following always fails:

DateTime dtNow = DateTime.Now;

// save dtNow to a SQL Database
....
// load it again into a variable called dtLoadedDateTime
....
if (dtLoadedDateTime == dtNow) {
// we never get here, cause the ticks are different
}

My question is, are there any utility classes which provide methods to
compare dates (up to a given precision)? I'm looking for methods like...

bool EqualToSecond(DateTime dtFirst, DateTime dtSecond){}

.... which should return true if the Dates equal up to a seconds precision. A
different number of ticks should be ignored (until there is so many of them
that the Seconds are affected).

If they don't exist, what would be the most effective way of writing them
(using the datetimes Hour, Minute, Second properties? using a TimeSpan?)

Any suggestions on this issue are very welcome. Thanks in advance!

/matthias
 
N

Nicholas Paldino [.NET/C# MVP]

Matthias,

Have you used the SqlDateTime structure? You can take your dtNow value,
pass it to the constructor of the SqlDateTime structure, and then store that
in the database. When you get the value back later for comparison, you
should be able to compare the values correctly.

If this is an isolated case, then this will work, but if you use this
kind of logic everywhere, you are going to have to rethink your strategy for
date storage/manipulation.

Hope this helps.
 
M

matthias s.

Hi Nicholas,

thanks a bunch, I'll look into it.

/matthias

Nicholas Paldino said:
Matthias,

Have you used the SqlDateTime structure? You can take your dtNow value,
pass it to the constructor of the SqlDateTime structure, and then store that
in the database. When you get the value back later for comparison, you
should be able to compare the values correctly.

If this is an isolated case, then this will work, but if you use this
kind of logic everywhere, you are going to have to rethink your strategy for
date storage/manipulation.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

matthias s. said:
Hi there,

I'm surely not the first one to have this problem. But searching the group
didn't yield any results, so here goes:

When storing DateTime values in an SQL Database (using a 'datetime'
field),
the value returned by a subsequent query never exactly matches the
DateTime
values inserted (I guess the DateTime struct has a different precision
then
what is acutally saved to the DB), thus the following always fails:

DateTime dtNow = DateTime.Now;

// save dtNow to a SQL Database
...
// load it again into a variable called dtLoadedDateTime
...
if (dtLoadedDateTime == dtNow) {
// we never get here, cause the ticks are different
}

My question is, are there any utility classes which provide methods to
compare dates (up to a given precision)? I'm looking for methods like...

bool EqualToSecond(DateTime dtFirst, DateTime dtSecond){}

... which should return true if the Dates equal up to a seconds precision.
A
different number of ticks should be ignored (until there is so many of
them
that the Seconds are affected).

If they don't exist, what would be the most effective way of writing them
(using the datetimes Hour, Minute, Second properties? using a TimeSpan?)

Any suggestions on this issue are very welcome. Thanks in advance!

/matthias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top