0

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?

1

2 Answers 2

4

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.

1
  • Code you suggesed: If objFile.DateLastModified = CDate("2012-10-19 11:34:06") Then Worked for me - thanks.
    – Mike
    Apr 18, 2013 at 9:49
2
  1. Use

    objFile.DateLastModified = DateSerial(2012,10,19) + TimeSerial(11,34,06)
    
  2. Use a Date literal (format #mm/dd/yyyy h:mm:ss AM/PM#, locale-independent):

    objFile.DateLastModified = #10/19/2012 11:34:06 AM#
    
  3. Use what xkcd tells you.

    objFile.DateLastModified = CDate("2012-10-19 11:34:06")
    
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.