-1

I am trying to display a message based on a date range. If it falls between the BeginDate and EndDate then the date should be display and if the date falls on 10-09-2017 then another message should be displayed, otherwise then it should display the date. This seems to fail and go directly to the else statement. My eyes are not picking up the error. How can something get displayed between a date range in VBScript.

    <%
    Dim DateT

    Dim BeginDate
    Dim EndDate

    BeginDate = Day("2017-05-26")

    EndDate = Day("2017-11-04")

    DateT = Day(Date)


     If BeginDate >= DateT =< EndDate
THEN response.write(DateT) 

    ElseIF  BeginDate  = Day("2017-10-09")THEN
        response.write(DateT) 

    Else 
    response.write(DateT)


    End If


    %>
user692942
  • 16,398
  • 7
  • 76
  • 175
Ana
  • 105
  • 7
  • Maybe you have to write with a little bit more detail, like this: **If (BeginDate >= DateT) And (DateT =< EndDate) THEN...** – statosdotcom Nov 02 '18 at 02:59
  • 1
    Why are you using `Day` at all? Why not just compare dates? – Nick.Mc Nov 02 '18 at 03:04
  • @Nick I used Day in order to pick up the day I am intending to target. You are saying that I can compare this two dates instead of using day ? – Ana Nov 02 '18 at 03:20
  • 1
    Yes. Use`CDate` to make sure its' a date. This syntax: `If BeginDate >= DateT =< EndDate` is not something I have ever seen before and I doubt it does what you think. Try `If DateT >= BeginDate AND DateT =< EndDate` instead – Nick.Mc Nov 02 '18 at 04:09
  • 3
    Possible duplicate of [Classic Asp date comparison not working correctly](https://stackoverflow.com/questions/18644293/classic-asp-date-comparison-not-working-correctly) – user692942 Nov 02 '18 at 07:48
  • 1
    Only [had a question](https://stackoverflow.com/q/53103332/692942) on exactly the same problem less than a day ago, people need to search before posting. – user692942 Nov 02 '18 at 07:59

1 Answers1

1

I believe your syntax is incorrect and your logic is overcomplicated.

Not exactly sure of the syntax here or the correct date format but I added some response.write for you to check (which should have been your first stop)

Your logic is overcomplicated because the only time it doesn't display the date is when it matches that special date..

Dim DateT

Dim BeginDate
Dim EndDate

BeginDate = CDate("2017-05-26")
EndDate = CDate("2017-11-04")
DateT = Date()

' remove these when you're finished debugging
    response.write(BeginDate) 
    response.write(EndDate) 

 If BeginDate  = CDate("2017-10-09")THEN
    response.write("Another message") 
 Else 
   response.write(DateT)
 End If
user692942
  • 16,398
  • 7
  • 76
  • 175
Nick.Mc
  • 18,304
  • 6
  • 61
  • 91
  • As Nick says, don't try comparing string representations of dates. As the string conversion to date is dependent on regional settings of the server running the ASP code. You should always use `IsDate()` to check a date string can be converted to a valid date and then use `CDate()` to convert it to a valid date before attemoting to compare. This isn't anything new, it's been [answered before](https://stackoverflow.com/a/18644498/692942) on [so] multiple times. – user692942 Nov 02 '18 at 07:53