0

I'm passing my variables in the following order: FileEndDate StartDateRange EndDateRange

FileEndDate = 10/11/2019
StartDateRange = 01/04/2019
EndDateRange = 30/04/2019

However, my code returns 'True', despite the fact that 10/11/2019 should not be in the date range of 01/04/2019 -> 30/04/2019.

If (WScript.Arguments.Item(0) >= WScript.Arguments.Item(1)) And (WScript.Arguments.Item(0) <= WScript.Arguments.Item(2)) Then
   WScript.Stdout.Writeline "True"
Else    
   WScript.Stdout.Writeline "False"
End If
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328

2 Answers2

0

You are comparing strings, "10/11/2019" > "01/04/2019" and "10/11/2019" < "30/04/2019".

You may want to work with dates instead, or rewrite the strings to make sure a simple string comparison will work (YYYY/MM/DD)

The latter can be done this way :

dim FileEndDate : FileEndDate = "10/11/2019"
dim StartDateRange : StartDateRange = "01/04/2019"
dim EndDateRange : EndDateRange = "30/04/2019"

function ReformatDate(sInputDate)
    dim aResult
    aResult = Split(sInputDate, "/")
    ReformatDate = aResult(2) & "/" & aResult(1) & "/" & aResult(0)
end function

FileEndDate = ReformatDate(FileEndDate)
StartDateRange = ReformatDate(StartDateRange)
EndDateRange = ReformatDate(EndDateRange)

If (FileEndDate >= StartDateRange) And (FileEndDate <= EndDateRange) Then
   MsgBox "True"
Else    
   MsgBox "False"
End If

This outputs "False"


Note that this doesn't check the initial format of the strings. You may need to write your own check function to ensure that it won't crash.

Cid
  • 14,968
  • 4
  • 30
  • 45
  • Why not [just use `SetLocale` to set the current regional settings](https://stackoverflow.com/a/19312026/692942) for a particular LCID then use `CDate()` to convert it to a `Date` variable? – user692942 Apr 16 '19 at 06:45
  • 1
    @Lankymart using date objects was my first idea, but I'm not used to handle dates in VBS and I prefered handling strings to allow an alphanum comparison. Feel free to edit my answer if you want to add details concerning this way – Cid Apr 16 '19 at 07:54
-2

You are comparing strings and not dates.

Let's try this: convert your date strings as date types, then compare them:

Dim myDateFormat As String = "dd/MM/yyyy"
Dim date0 As Date = Date.ParseExact(WScript.Arguments.Item(0).ToString(), myDateFormat, System.Globalization.CultureInfo.InvariantCulture)
Dim date1 As Date = Date.ParseExact(WScript.Arguments.Item(1).ToString(), myDateFormat, System.Globalization.CultureInfo.InvariantCulture)
Dim date2 As Date = Date.ParseExact(WScript.Arguments.Item(2).ToString(), myDateFormat, System.Globalization.CultureInfo.InvariantCulture)

If (date0 >= date1) And (date0 <= date2) Then
   WScript.Stdout.Writeline "True"
Else    
   WScript.Stdout.Writeline "False"
End If

Also, using this could help avoid ulterior mistakes:

Option Strict On
Option Explicit On

Have fun!

laancelot
  • 3,138
  • 2
  • 14
  • 21