How can I handle dates in AppleScript?

AppleScript provides a command in the Standard Additions to get the current date (called “current date”, go figure), and the date class.

As for AppleScript 1.10 (OS 10.4), the date class has the following properties:
-weekday
-month
-day
-year
-hours
-minutes
-seconds
-time
-date string
-short date string
-time string

Both “weekday” and “month” are also classes, which “contain” the classes January or Wednesday.

You can coerce some of these classes to string or number. For example, “January as number” should return the integer “1”.

Following some samples (our advice is that you practice yourself, and ask in a public forum if you have some problem):

--> NOTE: date format (strings) are formatted according to your language and preferences in the International preference pane.
--> The following results are valid in my computer, but may return different things in yours one.

set someDate to current date --> date "viernes, 27 mayo 2005 18:38:36"

set month of someDate to July
someDate --> date "miÃ?©rcoles, 27 julio 2005 18:38:36"

short date string of someDate --> "27/7/05"

set trickyDate to "01/01/01"
set trickyDate to date trickyDate --> date "lunes, 1 enero 2001 00:00:00"

someDate - trickyDate --> 144182316 (difference in seconds)

trickyDate + 2 * weeks + 2 * days + 2 * hours --> date "miÃ?©rcoles, 17 enero 2001 02:00:00"

month of trickyDate as number --> 1

AppleScript provides also the command “time to GMT”, which returns the difference in seconds between the current time zone and Universal Time (Greenwich Mean Time).

And you can allways use “do shell script” and the *nix tool “date” (type “man date” in a Terminal window for more info).