I’ve got a VBscript that runs on devices placed all over the world and therefore uses various regional settings.
A part of my script is calculating the difference between 2 dates, but I can’t get that working with a consistent result.
An example:
'Get system date in EU format
dEUDate = Right("0" & DatePart("d",Date), 2) & "-" & Right("0" & DatePart("m",Date), 2) & "-" & DatePart("yyyy",Date)
'Read date/time value from an application in registry - will return e.g. 05-01-2013 07-19-00
dateKLAVDef = UCase(objShell.RegRead(strKLAVStateKey & "\Protection_BasesDate"))
'Find date difference
iAVDefAge = DateDiff("d", Left(dateKLAVDef, 10), dEUDate)
WScript.Echo "AV def.: " & dateKLAVDef & vbTab & "Current date: " & dEUDate & vbTab & "Diff: " & iAVDefAge
This is the result when the device is configured with EU regional settings which gives the correct result:
AV def.: 05-01-2013 07-19-00 Current date: 07-01-2013 Diff: 2
This is the result when the device is configured with non-EU regional settings (e.g. English (United States)) which (from the scripts point of view) is incorrect:
AV def.: 05-01-2013 07-19-00 Current date: 07-01-2013 Diff: 61
How can I get this working with non-EU regional settings?
Thanks in advance.
It seems
dateKLAVDefis always in dd-MM-yyyy hh-mm-ss. Is that correct? Then you have to retrieve the day, month and year from this string and make it aDatewithDateSerial(y, m, d). It will automatically transform to the correct locale formatting. Do aDateDiffwith the current date et voila, you are ready: