Hello All,
having a bit of trouble getting this script correct. Was hoping the members of this wonderfully informative forum could help a beginner. I need the script to copy the date, 30 days prior to the current day in short form (DD/MM/YYYY) to the clipboard. This is what i have so far:
set theDay to day of (current date) as string
set theMonth to month of (current date) as integer
set theYear to year of (current date)
set shortdate to theMonth & “/” & theDay & “/” & theYear
(result as string)
set the clipboard to result
I’m guessing I will need to subtract seconds for when the year rollover comes? - 2592000 * seconds, also, when its a single digit day or month, if possible, I would need a zero before it so it always populate the full 8 digits every time. I have hit a wall Any help would be greatly appreciated!
Model: MacBook Pro
AppleScript: 2.3 (118)
Browser: Safari 533.21.1
Operating System: Mac OS X (10.6)
Try this…
set currentDate to current date
set earlierDate to currentDate - 30 * days
set theDay to (day of earlierDate) as text
set theMonth to ((month of earlierDate) as integer) as text
set theYear to (year of earlierDate) as text
set twoDigitDay to text -2 thru end of ("0" & theDay)
set twoDigitMonth to text -2 thru end of ("0" & theMonth)
set shortdate to twoDigitMonth & "/" & twoDigitDay & "/" & theYear
set the clipboard to shortdate
short version
set the clipboard to (do shell script "date -v -30d +%m/%d/%Y")
Hank’s script’s clearly over-written to help the OP understand a few AppleScript date-handling techniques.
I’d explicitly add that ‘current date’ is a function. Calling it three times for the same date is not only inefficient, but carries a slight risk that one of the dates will be different if the system clock should happen to tick over between calls.
The thing that depresses me in threads such as this is that the query clearly states:
Yet all the code ” including the OP’s own ” is directed towards MM/DD/YYYY.
It’s actually not clear because the code is different and from my experience
mostly the code reflects the intention of the OP
Thank you, this works beautifully! I really appreciate the help
Andrew