2015-11-06 144 views
1

我试图将文件的修改日期与特定日期进行比较。比较修改日期与特定日期(VBS)

什么我是这样的:

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

我使用则IsDate与#11/9 /到2015年#值的变量尝试,但始终返回false。我无法弄清楚如何将变量“specificDate”设置为11/9/2015。

+0

您不想使用'FormatDateTime()'比较'Date',因为该函数会根据您指定的格式生成日期的字符串表示形式。只比较'Date'变量只能在比较中使用'objFile.DateLastModified'。 – Lankymart

+0

这里有一些有用的信息 - http://stackoverflow.com/questions/16078043/vbs-objfile-datelastmodified-and-date-format-settings – Lankymart

回答

0

如果您仅比较日期(但不是时间),那么您必须切断最后修改值的小数部分,因为整数部分表示天数,小数部分表示小时,分钟和秒。在进行任何更改之后,将值转换回日期类型并使用CDate()以及比较前的包含字符串的日期。

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