Variable Help

I’m trying to get the year, date and day from the AppleScript current date command, in order to concatenate the results to use in a folder name (example: 20111003). However, I don’t understand how to get the results for lines 4 and 5 below (line 6, of course, returns “3”) and coerce them into a separate result. Can someone give me a quick pointer? Thanks.

set someYear to (the current date)
set someMonth to (the current date)
set someDay to (the current date)

year of someYear as number
month of someMonth as number
day of someDay as number

Hi,

in the first three lines you define three variables and assign three times the same value.
This extracts the day, month and year value


set {year:someYear, month:someMonth, day:someDay} to current date

Consider that the month is a enumerated constant.
To get the desired date string you could use this, the subroutine adds a leading zero if needed


set {year:someYear, month:someMonth, day:someDay} to current date
set myDate to (someYear as text) & pad(someMonth as integer) & pad(someDay)

-- subroutine
on pad(v)
	return text -2 thru -1 of ((100 + v) as text)
end pad

Thanks Stefan. That’s exactly what I needed. And thanks for the code that adds a leading zero, which is something that I was thinking about also.

Since the result you want looks like an eight-digit number, you could just do a bit of arithmetic and coerce the result to text:


set {year:someYear, month:someMonth, day:someDay} to current date
set myDate to (someYear * 10000 + someMonth * 100 + someDay) as text

Nigel, that’s a very interesting solution to the issue. If you can, is there a way to trim the “2011” down to simply “11”, in order to let “11” stand for the year?

with Nigel’s solution


set {year:someYear, month:someMonth, day:someDay} to current date
set myDate to (someYear * 10000 + someMonth * 100 + someDay - 20000000) as text

but in this case maybe the shell version is more convenient

set myDate to do shell script "/bin/date +%y%m%d"

Thanks Stefan. I had seen the shell script, but I didn’t know how to get the flags formatted in the correct way. It looks like the shell is the simplest application for what I need.