Dates & Times in AppleScripts

Hey Calion,

Dealing with dates is pretty simple; it’s just a bit verbose.

set _date to current date
set dateString1 to date string of _date # Date-String depends upon the system date setting.
set dateString2 to YYYYMMDD(_date, "/")
set dateString3 to YYYYMMDD(_date, "-")

set _date to date "Thursday, May 04, 1961 at 11:30:00 GMT-5"
set dateString4 to YYYYMMDD(_date, ".")

on YYYYMMDD(_date, _sep)
	set {oldTIDS, AppleScript's text item delimiters} to {AppleScript's text item delimiters, _sep}
	tell _date
		set dateString to (get {its year, text -2 thru -1 of ("0" & (its month as number)), text -2 thru -1 of ("0" & its day)}) as text
	end tell
	set AppleScript's text item delimiters to oldTIDS
	return dateString
end YYYYMMDD

set {year:yyyy, month:m, day:d, weekday:w} to (current date) # Per Nigel on this page.
yyyy
m as number
d
w as text

If you want something more straightforward then use the Satimage.osax or the shell.

Satimage.osax

set dateStringSIO to strftime (current date) into “%Y/%m/%d”

Using the shell

set dateStringShell to do shell script “date ‘+%Y/%m/%d’”

What is "�” supposed to be in "tell (current date) + 11 * days to �”, above?

It’s supposed to be a line-continuation character: “¬”. MacScripter’s text encoding has changed since Adam posted his article in 2007. I’ve just edited the code so that it displays properly.