VBS objFile.DateLastModified and Date Format Settings

vbscript

I'm trying to do some date modify check for a file in my VBS script. But it looks like comparison operation depend on date format which set on machine where script is runnnig. I have diverse machines where Regioanal & date settings could be Russian, English US, English UK and I need to run my VBS against all machines and being able to compare date correctly.

When I'm trying to use

If objFile.DateLastModified = cdate("19.10.2012 11:34:06") then 
do something
Else do something else
End IF

It seemingly works and doing correct comparison on machine with Russian formats setting, but fails on machine with English UK formats setting with following error

Type mismatch: 'cdate'
800A000D

If I use following DateSerial(2012,10,19) it doesn't throw an error but fail to compare dates correctly.

What is the best and easiest way to compare file modify date against predifined value with VBS irrespectively of machine date format setting?

Best Answer

Long story short: Don't use localized date formats when parsing dates.

ISO 8601 format works just fine:

If objFile.DateLastModified = CDate("2012-10-19 11:34:06") Then
  ' do something
Else
  ' do something else
End If

If you must, you can use the SetLocale() function explicitly to make your script run under different environments:

SetLocale 1049  ' LCID "Russian"
MsgBox CDate("5.4.2013 15:00:00")  ' April 5, shown as 05.04.2013 15:00:00

SetLocale 1033  ' LCID "English - United States"
MsgBox CDate("5.4.2013 15:00:00")  ' Type mismatch error

Refer to the list of assigned LCIDs on the MSDN.

Related Topic