Anyone know how to get the user’s date format?
Joe
jonn8
November 16, 2003, 6:50pm
#2
This is based on code passed along by jj & Fredo:
–Open this script in a new Script Editor window.
set {the_month , the_day , the_year } to words of (do shell script “date ‘+%m %d %Y’”)
set the_date to (my format_date (the_month , the_day , the_year ))
return "Today’s date is " & the_date
on [color=green]get_date_format/color
set date_formats to {“MM/DD/YYYY”, “DD/MM/YYYY”, “YYYY/MM/DD”, “YYYY/DD/MM”}
set test_dates to {“12/31/2000”, “31/12/2000”, “2000/12/31”, “2000/31/12”}
repeat with i from 1 to (length of test_dates )
try
get (date (item i of test_dates ))
return (item i of date_formats )
end try
end repeat
return “Could not determine date format.”
end get_date_format
on format_date (the_month , the_day , the_year )
set local_date_format to (my [color=green]get_date_format/color )
if local_date_format = “MM/DD/YYYY” then
set the_date to (the_month & “/” & the_day & “/” & the_year )
else if local_date_format = “DD/MM/YYYY” then
set the_date to (the_day & “/” & the_month & “/” & the_year )
else if local_date_format = “YYYY/MM/DD” then
set the_date to (the_year & “/” & the_month & “/” & the_day )
else if local_date_format = “YYYY/DD/MM” then
set the_date to (the_year & “/” & the_day & “/” & the_month )
else
return local_date_format
end if
return (the_date as string )
end format_date
Jon
[This script was automatically tagged for color coded syntax by Convert Script to Markup Code ]
Any way to find the seperator as well? And does this work for like the Chinese language?
Joe
jonn8
November 16, 2003, 10:07pm
#4
I’ve modified the code to get the customized separator as well but this requires Mac OS 10.3+ since it uses the new property of the date “short date string”. I’ve tested this by changing the region setting in the “Formats” tab of the International preference pane. It seems to work fine for Chinese (and other non-Roman script based languages) since the numbers remain in Roman script:
–Open this script in a new Script Editor window.
set {the_month , the_day , the_year } to words of (do shell script “date ‘+%m %d %Y’”)
set the_date to (my format_date (the_month , the_day , the_year ))
return "Today’s date is " & the_date
on [color=green]get_date_format/color
set date_formats to {(“MM” & s & “DD” & s & “YYYY”), (“DD” & s & “MM” & s & “YYYY”), (“YYYY” & s & “MM” & s & “DD”), (“YYYY” & s & “DD” & s & “MM”)}
set test_dates to {(“12” & s & “31” & s & “2000”), (“31” & s & “12” & s & “2000”), (“2000” & s & “12” & s & “31”), (“2000” & s & “31” & s & “12”)}
repeat with i from 1 to (length of test_dates )
try
get (date (item i of test_dates ))
return (item i of date_formats )
end try
end repeat
return “Could not determine date format.”
end get_date_format
on [color=green]get_date_format_separator/color
set the_short_date to characters of (short date string of (current date )) –requires 10.3+
repeat with i from 1 to (length of the_short_date )
if (item i of the_short_date ) is not in (characters of “0123456789”) then return (item i of the_short_date )
end repeat
return “Could not determine date format separator.”
end get_date_format_separator
on format_date (the_month , the_day , the_year )
set s to (my [color=green]get_date_format_separator/color )
set local_date_format to (my [color=green]get_date_format/color )
if local_date_format = (“MM” & s & “DD” & s & “YYYY”) then
set the_date to (the_month & s & “” & the_day & s & “” & the_year )
else if local_date_format = (“DD” & s & “MM” & s & “YYYY”) then
set the_date to (the_day & s & “” & the_month & s & “” & the_year )
else if local_date_format = (“YYYY” & s & “MM” & s & “DD”) then
set the_date to (the_year & s & “” & the_month & s & “” & the_day )
else if local_date_format = (“YYYY” & s & “DD” & s & “MM”) then
set the_date to (the_year & s & “” & the_day & s & “” & the_month )
else
return local_date_format
end if
return (the_date as string )
end format_date
Jon
[This script was automatically tagged for color coded syntax by Convert Script to Markup Code ]
Hm, thanks! Would the unix date command return the correctly formatted text to remove Panther dependencies?
From the date man page:
%x locale's date representation (mm/dd/yy)
Joe