17

Using ASP classic, I need to somehow compare two dates with each other. How can I do this?

MarkJ
  • 30,070
  • 5
  • 68
  • 111
poo
  • 1,095
  • 4
  • 13
  • 21

2 Answers2

24
Date1 = #rs["date"]#
Date2 = #12/1/2009#


If DateDiff("d", Date1, Date2) > 1 Then
    response.write "This date is before 12/1/2009"
Else
    response.write "This date is after 12/1/2009"
End If
Mikos
  • 8,455
  • 10
  • 41
  • 72
  • What's the benefit of using `DateDiff` instead of comparing the Dates directly? – John Smithers Feb 24 '10 at 15:43
  • 8
    You can choose the amount of difference that's significant (days, months, weeks). – Broam Feb 24 '10 at 15:44
  • 1
    +1 to Broam's response, plus IMHO it is better programming practice to compare on the parameter you can compare on vs. a "blind" compare. – Mikos Feb 24 '10 at 20:51
17
If Date1 > Date2 Then
  ' Date1 occurred after Date 2
End If

Use >, < and = like comparing numbers (and >=, <= and <> too). Smaller dates are more historic.

This of course assumes that Date1 and Date2 are actually Date or DateTime objects. If they aren't, you'll need to convert them to Date objects first using CDate().

Welbog
  • 59,154
  • 9
  • 110
  • 123
  • 7
    In vbscript, they are always variants, however IsDate() can be used to check that the values can be implicitly cast as dates - often worthwhile IMHO. – CJM Feb 24 '10 at 15:56