DateTime.Compare(t1, t2)???

  • Thread starter Thread starter Darryn Ross
  • Start date Start date
D

Darryn Ross

Hi,

I am having problems with some date comparisons.... i am not getting the
correct answer when doing DateTime.Compare(t1, t2) ;

I have a DataGrid full of records that i have bound using a currency manager
to a DataTable.

Records in the Datagrid can be edited added or deleted.

before i save the any modified transactions in the grid i validate each and
every one to enure they meet certain criteria, this is where my problem is
appearing. I compare the date in my transaction to a stored date i have to
see if it is within a valid range, however when i compare dates of the same
date the compare function is not returning 0 it is returning -1?? i cannot
understand what it is doing.. i have included a small snippet of my validate
function where the error is happening.

row 0 is my first row.
column 2 is my Date Column in my Grid.
dtValidate is a DateTime object.

dtValidate = Convert.ToDateTime(datagrid[0, 2]) ;

//The calculation error occurs here..

i = DateTime.Compare(dtValidate, DateStart) ; // This should return 0 but it
returns -1 both dates are exactly the same?? {07/01/2004} "MM/dd/yyy"

Any help would be appreciated...

Regards

Darryn
 
Hi Darryn

When you are comparing, the time part of the date might not be
matching. Try displaying dtValidate and DateStart to the screen and you
will see the difference in the time part of it.

Use

i = DateTime.Compare(dtValidate.toShortDateString(),
DateStart.toShortDateString());

Regards,
Lalasa.
 
Sorry, that would not work as the arguments can not be strings.

I would rather compare the strings, if the times do not match.

string date1 = dtValidate.toShortDateString();
string date2 = DateStart.toShortDateString();

bool bMatch = date1.equals(date2)

-Lalasa.
 
Darryn,
As L suggests, are you certain both dtValidate & DateStart do not have time
values associated with them?

One way to ensure you are comparing just the date portions of the DateTime,
is to use the Date property:
i = DateTime.Compare(dtValidate.Date, DateStart.Date) ;

Hope this helps
Jay

Darryn Ross said:
Hi,

I am having problems with some date comparisons.... i am not getting the
correct answer when doing DateTime.Compare(t1, t2) ;

I have a DataGrid full of records that i have bound using a currency
manager
to a DataTable.

Records in the Datagrid can be edited added or deleted.

before i save the any modified transactions in the grid i validate each
and
every one to enure they meet certain criteria, this is where my problem is
appearing. I compare the date in my transaction to a stored date i have to
see if it is within a valid range, however when i compare dates of the
same
date the compare function is not returning 0 it is returning -1?? i cannot
understand what it is doing.. i have included a small snippet of my
validate
function where the error is happening.

row 0 is my first row.
column 2 is my Date Column in my Grid.
dtValidate is a DateTime object.

dtValidate = Convert.ToDateTime(datagrid[0, 2]) ;

//The calculation error occurs here..

i = DateTime.Compare(dtValidate, DateStart) ; // This should return 0 but
it
returns -1 both dates are exactly the same?? {07/01/2004} "MM/dd/yyy"

Any help would be appreciated...

Regards

Darryn
 
Hi

ok, i used

i = DateTime.Compare(dtValidate.Date, DateStart.Date) ;

instead of

i = DateTime.Compare(dtValidate, DateStart) ;

and it worked great, i still am not sure why it was not working before as i
am almost certain there was no time stamp within the DateTime object?

thanks for all your help Jay & L

Regards

Darryn


Jay B. Harlow said:
Darryn,
As L suggests, are you certain both dtValidate & DateStart do not have time
values associated with them?

One way to ensure you are comparing just the date portions of the DateTime,
is to use the Date property:
i = DateTime.Compare(dtValidate.Date, DateStart.Date) ;

Hope this helps
Jay

Darryn Ross said:
Hi,

I am having problems with some date comparisons.... i am not getting the
correct answer when doing DateTime.Compare(t1, t2) ;

I have a DataGrid full of records that i have bound using a currency
manager
to a DataTable.

Records in the Datagrid can be edited added or deleted.

before i save the any modified transactions in the grid i validate each
and
every one to enure they meet certain criteria, this is where my problem is
appearing. I compare the date in my transaction to a stored date i have to
see if it is within a valid range, however when i compare dates of the
same
date the compare function is not returning 0 it is returning -1?? i cannot
understand what it is doing.. i have included a small snippet of my
validate
function where the error is happening.

row 0 is my first row.
column 2 is my Date Column in my Grid.
dtValidate is a DateTime object.

dtValidate = Convert.ToDateTime(datagrid[0, 2]) ;

//The calculation error occurs here..

i = DateTime.Compare(dtValidate, DateStart) ; // This should return 0 but
it
returns -1 both dates are exactly the same?? {07/01/2004} "MM/dd/yyy"

Any help would be appreciated...

Regards

Darryn
 
Darryn,
| and it worked great, i still am not sure why it was not working before as
i
| am almost certain there was no time stamp within the DateTime object?
You can use DateTime.TimeOfDay to display the time portions of each
DateTime. Depending on where they are coming from, one of them must include
a Time portion.

Debug.WriteLine(dtValidate.TimeOfDay, "dtValidate")
Debug.WriteLine(DateStart.TimeOfDay, "DateStart")

Hope this helps
Jay

| Hi
|
| ok, i used
|
| i = DateTime.Compare(dtValidate.Date, DateStart.Date) ;
|
| instead of
|
| i = DateTime.Compare(dtValidate, DateStart) ;
|
| and it worked great, i still am not sure why it was not working before as
i
| am almost certain there was no time stamp within the DateTime object?
|
| thanks for all your help Jay & L
|
| Regards
|
| Darryn
|
|
| | > Darryn,
| > As L suggests, are you certain both dtValidate & DateStart do not have
| time
| > values associated with them?
| >
| > One way to ensure you are comparing just the date portions of the
| DateTime,
| > is to use the Date property:
| >
| > > i = DateTime.Compare(dtValidate.Date, DateStart.Date) ;
| >
| > Hope this helps
| > Jay
| >
| > | > > Hi,
| > >
| > > I am having problems with some date comparisons.... i am not getting
the
| > > correct answer when doing DateTime.Compare(t1, t2) ;
| > >
| > > I have a DataGrid full of records that i have bound using a currency
| > > manager
| > > to a DataTable.
| > >
| > > Records in the Datagrid can be edited added or deleted.
| > >
| > > before i save the any modified transactions in the grid i validate
each
| > > and
| > > every one to enure they meet certain criteria, this is where my
problem
| is
| > > appearing. I compare the date in my transaction to a stored date i
have
| to
| > > see if it is within a valid range, however when i compare dates of the
| > > same
| > > date the compare function is not returning 0 it is returning -1?? i
| cannot
| > > understand what it is doing.. i have included a small snippet of my
| > > validate
| > > function where the error is happening.
| > >
| > > row 0 is my first row.
| > > column 2 is my Date Column in my Grid.
| > > dtValidate is a DateTime object.
| > >
| > > dtValidate = Convert.ToDateTime(datagrid[0, 2]) ;
| > >
| > > //The calculation error occurs here..
| > >
| > > i = DateTime.Compare(dtValidate, DateStart) ; // This should return 0
| but
| > > it
| > > returns -1 both dates are exactly the same?? {07/01/2004} "MM/dd/yyy"
| > >
| > > Any help would be appreciated...
| > >
| > > Regards
| > >
| > > Darryn
| > >
| > >
| >
| >
|
|
 

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

Back
Top