1

I'm trying to compare a file's modified date with a specific date.

What I have is this:

If FormatDateTime(objFile.DateLastModified,vbShortDate) = specificDate Then
       'Do something
End if

I've tried using IsDate and a variable with a value of #11/9/2015# but always returns false. I can't figure out how to set the variable "specificDate" to 11/9/2015.

user692942
  • 16,398
  • 7
  • 76
  • 175
tchthsky
  • 13
  • 4
  • You don't want to compare a `Date` using `FormatDateTime()` because that function generates a string representation of the date to the format you specify. Only compare `Date` variables only use `objFile.DateLastModified` in the comparison. – user692942 Nov 06 '15 at 18:40
  • Some useful info here - http://stackoverflow.com/questions/16078043/vbs-objfile-datelastmodified-and-date-format-settings – user692942 Nov 06 '15 at 18:42

1 Answers1

0

If you are comparing the date only (but not the time), then you have to cut off the fractional part of the last modified value, since integer part represents days, and fractional part - hours, minutes and seconds. After any changes convert the values back to Date type with CDate(), as well as date containing string before the comparison.

Sub Test()
    dtSpecificDate = CDate("11/9/2015")
    With CreateObject("Scripting.FileSystemObject").GetFile("C:\Test\tmp.txt")
        dtLastModified = CDate(Int(.DateLastModified))
    End With
    If dtLastModified = dtSpecificDate Then
        ' Do something
    End If
End Sub
omegastripes
  • 12,351
  • 4
  • 45
  • 96