with the command:

set timeStamp to do shell script “date "+%Y%m%d_%H%M%S"”

I can get the current date formatted as YYYYMMDD_HHMMSS with a single line command that substitute a lot of AS code.

I would like to pass to the do shell script a date, for example the creation date or modification date of a file and get the same formatting.
Is this possible? How I can pass an AS Date to the do shell script?



there are several approaches, here is one

set aDate to (current date)
tell aDate to set dateString to ((year as text) & my pad(its month as integer)) & my pad(its day) & ¬
	"_" & my pad(its hours) & my pad(its minutes) & my pad(its seconds)
on pad(t)
	return text -2 thru -1 of ((100 + t) as text)
end pad

That would take quite a lot of code itself! If you’re starting with an AppleScript date, it’s easiest and fastest to use AppleScript to deal with it. The amount of code is immaterial. Stefan’s script does the job very well, or there’s this variant:

on dateToTimeStamp(aDate)
	tell aDate to return (((its year) * 10000 + (its month) * 100 + (its day)) as text) & "_" & text 2 thru -1 of ((1000000 + (its hours) * 10000 + (its minutes) * 100 + (its seconds)) as text)
end dateToTimeStamp

dateToTimeStamp(current date)