Question

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.

Was it helpful?

Solution

It seems dateKLAVDef is 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 a Date with DateSerial(y, m, d). It will automatically transform to the correct locale formatting. Do a DateDiff with the current date et voila, you are ready:

' Read date/time value from an application in registry - will 
' return e.g. 05-01-2013 07-19-00
dateKLAVDef = "05-01-2013 07-19-00"

' Define a pattern for the given string
dateKLAVDefPattern = "^(\d+)-(\d+)-(\d+) .*"

' Make a regular expression
Set re = new RegExp
re.Pattern = dateKLAVDefPattern

' Execute the regex
Set matches = re.Execute(dateKLAVDef)

' Retrieve the submatches from the match
dateKLAVDefDay = matches(0).submatches(0)
dateKLAVDefMonth = matches(0).submatches(1)
dateKLAVDefYear = matches(0).submatches(2)

' recreate the dateKLAVDef date as a real date object
dateKLAVDefAsDate = DateSerial(dateKLAVDefYear, dateKLAVDefMonth, dateKLAVDefDay)

' Do a datediff on the created date and the current date
iAVDefAge = DateDiff("d", dateKLAVDefAsDate, Date())
MsgBox iAVDefAge
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top