Wednesday, 12 March 2014

Apple: Get Date and Time with AppleScript

<< Current Date >>

This Applescript gets the current date: current date.

Just type those 2 words into a blank Script Editor document and click the Run button. In the Result pane at the bottom of the window you’ll see a line like date "Monday 16 August 2010 20:47:32 ".

The result includes the word ‘date’ and the speechmarks.

If all you wanted was the first part, Monday 16 August 2010, then you need to work with it a bit. You need to get the date string.

To get the time portion, 20:47:32 you’d get the time string.

Note: the symbol ¬ shows where you must press the Return key to make a new line.


set myDate to date string of (current date) ¬
myDate

set myTime to time string of (current date) ¬
myTime

set myWords to myTime & " " & myDate ¬
myWords


The result of that sequence of commands is the line "20:47:32 Monday 16 August 2010".


<< Tomorrow's Date >>

The date itself is stored as a bunch of numbers. Because they’re numbers you can do maths operations like add and subtract. Once you get the string the date becomes a bunch of words.

So, to get tomorrow’s date (I don’t need to care about the time), I need to find the Current Date and add 60 seconds * 60 minutes * 24 hours, or as I write it: (24*60*60).

set Tomorrow to (current date) + (24 * 60 * 60) ¬

set myTomorrow to (date string of Tomorrow)


The result of that sequence of commands when I run it on Tuesday, 17 August 2010 is the line "Wednesday 18 August 2010".


<< Short Date >>

In the file where I keep a record of each item I use I want the date like this: 20100817. That format would allow me to sort easily.

To get that format I need to get, and then work with, the Short Date in Applescript.

The Short Date string alone gets something like this: "18/08/2010", but that’s not quite what I want.

To turn the date around I have to pick out each bit, using code like ((items 4 through 5 of ShortDate) as string). Items 4 through 5 net me the 08 portion.

Here’s the code from my script:

set myTomorrow to ((current date) + (24 * 60 * 60)) ¬

set ShortDate to short date string of myTomorrow ¬

set stampText to "20" & ((items 9 through 10 of ShortDate) as string) & ((items 4 through 5 of ShortDate) as string) & ((items 1 through 2 of ShortDate) as string)

Out of all that I get stampText, which is the string I need in my preferred format: 20100817.


Reference:
Today and Tomorrow in Applescript
http://knowit.co.nz/2010/08/today-and-tomorrow-in-applescript

14 comments:

  1. Thanks, very nice article.

    how would i write a script that can check a file's datestamp and if it is older than 30 days it will show a popup message?

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Andres Cheah: Apple: Get Date And Time With Applescript >>>>> Download Now

    >>>>> Download Full

    Andres Cheah: Apple: Get Date And Time With Applescript >>>>> Download LINK

    >>>>> Download Now

    Andres Cheah: Apple: Get Date And Time With Applescript >>>>> Download Full

    >>>>> Download LINK

    ReplyDelete