Date Formatting
Date Formatting
- Subject: Date Formatting
- From: <email@hidden>
- Date: Fri, 7 Mar 2003 13:45:22 -0000
Dear Applescripters,
I've noticed that many of the recent date formatting posts concentrate on
OS X and the unix date command. This is easy but not much help to OS 9 users.
What about this old AS guidebook function? It may not be as short, but its
reasonably clear, it works in 9, and has kept right on working for me in X
without adjustment. It also handily and flexibly addresses the leading zeros
problem, and the US vs. World notation problem. Is there a reason that its
been considered unsuitable?
Sincerely,
Joshua See
on format_date_using(this_date_record, delimiter_string, format_list)
-- get the numeric day
set numeric_day to the day of this_date_record
-- get the month
set month_name to the month of this_date_record
-- convert month to number
set the list_of_months to {January, February, March, April [NOBREAK]
, May, June, July, August, September, October, November, December}
repeat with i from 1 to 12
if item i of the list_of_months is the month_name then
set the numeric_month to i
exit repeat
end if
end repeat
-- get the numeric year as text
set numeric_year to the year of this_date_record as string
set the formatted_date to ""
-- count the number of items in the list
set the item_count to the count of the format_list
-- parse the format list
repeat with i from 1 to the item_count
set this_item to item i of the format_list
if this_item is "D" then
set the formatted_date to [NOBREAK]
the formatted_date & numeric_day as string
else if this_item is "DD" then
if the numeric_day is less than 10 then [NOBREAK]
set numeric_day to [NOBREAK]
("0" & (the numeric_day as string))
set the formatted_date to [NOBREAK]
the formatted_date & numeric_day as string
else if this_item is "M" then
set the formatted_date to [NOBREAK]
the formatted_date & numeric_month as string
else if this_item is "MM" then
if the numeric_month is less than 10 then [NOBREAK]
set numeric_month to [NOBREAK]
("0" & (the numeric_month as string))
set the formatted_date to [NOBREAK]
the formatted_date & numeric_month as string
else if this_item is "YY" then
set the formatted_date to [NOBREAK]
the formatted_date & [NOBREAK]
((characters 3 thru 4 of numeric_year) as string)
else if this_item is "YYYY" then
set the formatted_date to [NOBREAK]
the formatted_date & numeric_year
end if
if i is not the item_count then
-- add delimiter
set the formatted_date to [NOBREAK]
the formatted_date & delimiter_string as string
end if
end repeat
return the formatted_date
end format_date_using
set the date_slug to my [NOBREAK]
format_date_using(the current date, "/", {"DD", "MM", "YY"})
--> RETURNS: "07/03/03"
set the date_slug to my [NOBREAK]
format_date_using(the current date, ".", {"MM", "DD", "YY"})
--> RETURNS: "07.03.03"
set the date_slug to my [NOBREAK]
format_date_using(the current date, "/", {"D", "M", "YYYY"})
--> RETURNS: "7/3/2003"
set the date_slug to my [NOBREAK]
format_date_using(the current date, "/", {"MM", "YY"})
--> RETURNS: "07/03"
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.