Regional date settings

Anything Really. Just keep it clean!

Moderators: Dorian (MJT support), JRL

Post Reply
User avatar
JRL
Automation Wizard
Posts: 3497
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Regional date settings

Post by JRL » Tue Feb 05, 2013 1:59 pm

I'm having a significant issue with a script I wrote failing because the regional date settings on other computers are not the same as on mine. The exact issue is that I wrote a script to deal with dates in a mm/dd/yyyy format and I've found a computer with dates set to mm-dd-yy. This causes difficulty when the script parses the date to put it into a mmddyy format for an EDI flat file. Macro Scheduler and VBScript date function results are all influenced by the computer's Regional Date Settings.

I was thinking it might be nice to have a Macro Scheduler system variable that defined the date/time format used by the date/time functions. I really don't know enough about the subject to know whether that would be a good idea or even if it would be necessary.

There are a significant number of Macro Scheduler users who write code to be distributed to the public or otherwise to non-locked-down computers. How do you folks deal with Regional Date Settings discrepancies?

User avatar
Marcus Tettmar
Site Admin
Posts: 7378
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue Feb 05, 2013 3:45 pm

Not ALL Macro Scheduler and VBScript date function results are influenced by regional settings. GetDate is. But Year, Month and Day always get the same results regardless of regional settings and DatePart and TimePart are also format independent (as long as the format provided is a recognised date format). In other words, don't use functions like GetDate. Use Year, Month, Day or if using VBScript use similar functions or date formatting functions which do not depend on a specific format.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
JRL
Automation Wizard
Posts: 3497
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Tue Feb 05, 2013 4:09 pm

Thanks Marcus.

Yes... when I want today's date I always use Day>, Month> and Year> then construct the date in the format I desire. I can't think of the last time I used GetDate> in one of my scripts. My problem today is that I wrote a lengthy process to make sure parts ship the day before a weekend or holiday so I'm using DateDiff> and DateAdd> which both require LocalDate> which is contingent on Regional settings. Same issue if I use the similar functions from VBScript. The results are formatted to the date time settings on the computer running the program.

I did just discover the VBScript function called FormatDateTime. It looks like it could resolve my immediate problem. I'll also look at DatePart> which looks promising as well. But the question remains. What has everyone else been doing in this situation?

User avatar
Marcus Tettmar
Site Admin
Posts: 7378
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue Feb 05, 2013 4:15 pm

Right. Yes, DateDiff and DateAdd need the date in local settings. So you use DateLocal to PUT a date into local settings. THEN you can use DateDiff. And THEN you can use DatePart if you want to strip that date out and reformat it

DateLocal takes year, month and day part and returns a format that the local system understands. So you can then use that in any calcs, and then use DataPart if you want to get the bits out and make your own format again.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
JRL
Automation Wizard
Posts: 3497
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Tue Feb 05, 2013 5:54 pm

...then use DataPart if you want to get the bits out and make your own format again.
It appears that DatePart> uses the least characters (1 or 2) for the month and day and 4 characters for the year.


So... if my local date looks like 02-15-13

DatePart> will give me 2, 15 and 2013. I can then pad zeros on to month and day if I need them and truncate the year if I only want a two character year.

Will DatePart> ALWAYS give me month and day with no leading zeros and a four character year?

User avatar
Marcus Tettmar
Site Admin
Posts: 7378
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Wed Feb 06, 2013 1:38 pm

Should do yes. Should be consistent. But you could always left pad with zeros and extract right two digits and that way you know you'll ALWAYS have 2 digits.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts