Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Locale settings and datediff calculation errors

Status
Not open for further replies.

prinand

MIS
Jun 13, 2000
98
GB
we have a global network so servers around the globe have different configurations. one of the most annoying issues when scripting for all of those servers is the datetime formatting.

some servers show 12/8/2010 1:30:00 PM
others 8/12/2010 13:30
Or 2010-12-08 13:30

what is the best solution to have reliable time calculations around the globe with one script ?

because sometimes 12/8 is interpeted as 8 December and sometimes as 12 August.

how can I script in a way which will work around the globe and prevent incorrect date interpetations ?

I am now trying to resolve it by using :
setlocale("en-gb")
T = formatdatetime(now,vbLongDate) & " " & Formatdatetime(now,vbLongTime)

which results in
T = 8 December 2010 13:30:00
which I store in a registry key.

another script that is accessing this value also uses the setlocale("en-gb")
hopefully the datediff can then only produce one answer

But I have run into issues with datetime formatting before.

So I would like to hear what other people do to overcome ambiguous answers when testing on dates

 
I compose my own. When you extract with the built in functions and then recompose them in the order you want, you don't have to deal with localization at all. The only thing you might want to take into account is timezone, which can be recovered as a numeric value (in minutes) from the registry.

Code:
Dim dCurrent, iDay, iMonth, iYear, iHour, iMinute, iSecond
dCurrent = Now

iDay	= Right("0" & Day(dCurrent), 2)
iMonth	= Right("0" & Month(dCurrent), 2)
iYear	= Year(dCurrent)
iHour	= Right("0" & Hour(dCurrent), 2)
iMinute	= Right("0" & Minute(dCurrent), 2)
iSecond	= Right("0" & Second(dCurrent), 2)

WScript.Echo iDay & "-" & iMonth & "-" & iYear & " " & iHour & ":" & iMinute & ":" & iSecond

PSC
[—] CCNP (R&S/Wireless) [•] CCSP [•] MCITP: Enterprise Admin [•] MCSE [—]

Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! --Mr. The Plague, from the movie "Hackers
 
i go with two approaches:
1. as PScottC describes
2. always include the SetLocale(_knownvalue)_

sometimes you have to convert other applications timestamp formats back into vbscripts SetLocale version or your format, i find the SetLocale helps

I Hear, I Forget
I See, I Remember
I Do, I Understand

Ronald McDonald
 
Thanks,

I will perform some testing detecting the locale (getlocale) and getting the day month year while using the locale is at the system default, and then convert that to the locale formatting of my liking.

 
i wouldnt go into detecting the locale, you dont need to worry about this, just use the SetLocale at the top of your script, then you know, as far as your script is concerned what the format from Date() Now() etc is going to be returned as

I Hear, I Forget
I See, I Remember
I Do, I Understand

Ronald McDonald
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top