Handling international date formats

I am trying to extract a date from a given string. Below I show how the date is formed from the string. This is all fine for those in the US or UK but for countries like Norway, where there is a period after the date, such a January 4th 2010 - would be written as 4. januar 2010. Do I need to put the syntax in for every country listed in the International preferences tab to handle these countries?


set reg_string to "05-22-2010dde"-- 05 is May 22 is day and 2010 is year
set theMonthNumber to characters 1 thru 2 of reg_string as string
set theDate to (current date)
set the month of theDate to theMonthNumber
set _month to month of theDate as string
set _day to characters 4 thru 5 of reg_string as string
set _year to characters 7 thru 10 of reg_string as string
try
	set date_string to _month & " " & _day & " " & _year
	set reg_date to date date_string
on error
	set date_string to _day & " " & _month & " " & _year
	set reg_date to date date_string
	
end try


Man, are you making it hard ;).

This is ‘auto-localising’ (and not mine):

set reg_string to "05-22-2010dde" -- 05 is May 22 is day and 2010 is year
set theDate to (current date)

tell theDate -- by Nigel Garvey, http://macscripter.net/viewtopic.php?pid=129037
	set its day to 1 -- overflow precaution
	set its year to text 7 thru 10 of reg_string -- automatic .
	set its month to text 1 thru 2 of reg_string -- . text to integer .
	set its day to text 4 thru 5 of reg_string -- . coercions
end tell

theDate --> date "zaterdag 22 mei 2010 12:19:16"

It should work in Norway. The dot is just a separator.

Extension:

tell theDate to set regDate to its short date string -->"22-05-2010", my short date, separator included

Thanks Alastor,

After some more poking around, I thought about temporarily changing the user locale, check the date in US format, and revert back to the original locale as such:


do shell script "defaults read .GlobalPreferences AppleLocale"
set originalDateFormat to result
do shell script "defaults write -g AppleLocale -string en_US"

-- check the dates, then set the original settings

do shell script "defaults write -g AppleLocale -string " & originalDateFormat


I think your method is much better, however. thank you!
Marlon

As you may have discovered, all methods that read some plist to find the date format will fail if it was never changed, because the relevant key/value pairs will be missing.
Starting from ‘current date’ will always work.

And you should thank Nigel “Date Master” Garvey for the core routine :smiley: