473,434 Members | 1,397 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,434 software developers and data experts.

Comparing Dates

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
Nov 17 '05 #1
2 1854
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]
- mv*@spam.guard.caspershouse.com

"matthias s." <postamt[theat]emvoid[thedot]de> wrote in message
news:e5**************@TK2MSFTNGP12.phx.gbl...
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

Nov 17 '05 #2
Hi Nicholas,

thanks a bunch, I'll look into it.

/matthias

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> schrieb
im Newsbeitrag news:#g**************@TK2MSFTNGP14.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"matthias s." <postamt[theat]emvoid[thedot]de> wrote in message
news:e5**************@TK2MSFTNGP12.phx.gbl...
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


Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: F | last post by:
Hi I have posted the question few days back about problem in inserting the dates in SQL server and thankful to them who replied. That was solved and this is a nice solution....
2
by: Mike N. | last post by:
I am currently using a function to validate a form on the client side (see code below). At the end of the function, I would like it to also compare a startDate against an endDate to ensure that the...
2
by: Duppypog | last post by:
I'm trying to compare a date stored in a database with today's date using an If statement, but it's not returning true. Example, value in database is 11/5/2003 with today being 11/6/2003. Can...
2
by: Philip Townsend | last post by:
I am having difficulty with a simple routine as follows: public static bool VerifyExpirationDate(DateTime date) { if(date>=DateTime.Now)return true; else return false; } The problem is that...
2
by: Manny Chohan | last post by:
Hi, i have two datetime values in format 11/22/04 9:00 AM and 11/22/04 9:30 AM. How can i compare dates .net c# or if there is any other way such as Javascript. Thanks Manny
2
by: ameshkin | last post by:
Hi GUys, Im trying to compare two dates in MYSQL. But its not treating the dates as numbers, but as strings. I try using strtotime but that did not work. Basically, if the last comment is...
12
by: colincolehour | last post by:
I am new to Python and am working on my first program. I am trying to compare a date I found on a website to todays date. The problem I have is the website only shows 3 letter month name and the...
4
by: cheryl | last post by:
I am using the PHP.MYSQL and Apache server application in developing my website. I have problem in comparing dates. Website has room reservation, the user will check first the room availability. The...
2
by: dantebothermy | last post by:
Is there a simple way to subtract the time from a datetime field, so when I compare dates, I'm always comparing the dates at 12:00 am? I'd really prefer not to convert all my dates to strings. ...
4
by: jupi13 | last post by:
i have this code..i don't know what where is the error in this one..it says data type mismatch..... Dim Mydate As Date Dim MydateString As String MydateString = "Text1.Text" Mydate =...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.