Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Comparing dates - seems to ignore the year 1

Status
Not open for further replies.

gavin333

Programmer
Jun 20, 2001
1
AU
Hi,
We're constructing a website using ASP and VBScript and
pulling data from a database that is date specific.

The below error checking code works for all dates within a particular year but if you change the year value where the variable DateTo is still < DateFrom an error message is displayed!
Also tried comparing DateValue(DateTo) DateValue(DateFrom) but we got the error every time then. We tested and it seemed to confirm that the if DD/MM < DD2/MM2 was true then process irrespective of the year (YY) value.

if (isDate(DateFrom) = True) and (isDate(DateTo)=True) then
if (DateTo < DateFrom) Then
StrError = StrError &&quot;<br>Date to is before date from&quot;
NoError = NoError + 1
elseif (DateValue(DateTo) < DateValue(DateFrom) and TimeValue(DateTo) < TimeValue(DateFrom)) Then
StrError = StrError &&quot;<br>Date to is before date from&quot;
NoError = NoError + 1
end if
end if

Any suggestions would be greatly appreciated thanks..
Regards
Gavin

 
Try this (it just might work!!)
Code:
Dim dteDateFrom
Dim dteDateTo

if (isDate(DateFrom) = True) and (isDate(DateTo)=True) then
   dteDateFrom = DateValue(DateFrom) 'OR try CDate(DateFrom)
   dteDateTo = DateValue(DateTo) 'OR try CDate
   if (dteDateTo < dteDateFrom) Then
      StrError = StrError &&quot;<br>Date to is before date from&quot;
      NoError = NoError + 1
I think you're always falling in here because you're comparing two string values instead of two date values (because you haven't converted them to dates like I did above. Make sense?
Code:
   elseif (DateValue(DateTo) < DateValue(DateFrom) and TimeValue(DateTo) < TimeValue(DateFrom))  Then
      StrError = StrError &&quot;<br>Date to is before date from&quot;
      NoError = NoError + 1
   end if
end if

Below is from MSDN library regarding CDate. Maybe you won't have to do the TimeValue stuff if you use CDate?:

&quot;CDate recognizes date literals and time literals as well as some numbers that fall within the range of acceptable dates. When converting a number to a date, the whole number portion is converted to a date. Any fractional part of the number is converted to a time of day, starting at midnight.&quot;...

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top