Wow!
Welcome to MacScripter.
There’s actually no need to convert to US format if the ultimate goal’s a ‘date’ variable. You only need to extract the figures from the text and set the appropriate properties of a date object. One way to get a date object to modify is to use the ‘current date’ function. (A date object can also be written and compiled directly into your script in your own machine’s local format. It will appear on other machines in their local formats. But the ‘current date’ method’s better when distributing code in text form, as here.)
on moreOrLessISO8601ToDate(iso8601)
-- Get a string containing just the digit characters from the input.
set digitsOnly to (do shell script "sed 's/[^0-9]//g' <<<" & quoted form of iso8601)
set digitCount to (count digitsOnly)
if (digitCount is 8) then -- Assume only the date is included.
tell digitsOnly to set {y, m, d, hr, min, sec} to {text 1 thru 4, text 5 thru 6, text 7 thru 8, 0, 0, 0}
else if (digitCount is 14) then -- Assume both the date and the time are included.
tell digitsOnly to set {y, m, d, hr, min, sec} to {text 1 thru 4, text 5 thru 6, text 7 thru 8, text 9 thru 10, text 11 thru 12, text 13 thru 14}
else -- Don't know how to interpret the input.
error
end if
-- Get a date object and modify it appropriately.
-- Its day should be set below 29 first to avoid any overflow during the other settings.
-- The numeric texts are automatically coerced to integers during the setting.
tell (current date) to set {its day, its year, its month, its day, its hours, its minutes, its seconds, dateObject} to {1, y, m, d, hr, min, sec, it}
return dateObject
end moreOrLessISO8601ToDate
set dateVariable to moreOrLessISO8601ToDate("2013-01-31 12:31:57")
There are system calls which can read the user’s Date/Time preferences, as Shane has said. There are examples using shell scripts in other threads on this site. You still have to use that information yourself though.
A simple vanilla way to deduce the user’s preferred short-date order is to set up a date specifier using a known short date and see what gets set to what in the resulting date object:
-- Create a date object from the short-date string "1 2 3".
set testDate to date ("1 2 3" as text)
-- Get the year, month, and day values from the date object.
-- The prior subtraction of the time is a precaution against a current AppleScript date bug.
set {year:y, month:m, day:d} to testDate - (testDate's time)
-- Arrange "year", "month", and "day" into a list in the order suggested by the respective values.
set shortDateOrder to {missing value, missing value, missing value}
tell shortDateOrder to set {item (y mod 10), item (m as integer), item d} to {"year", "month", "day"}
return shortDateOrder
--> {"day", "month", "year"} on my machine