O'Reilly logo

AppleScript in a Nutshell by Bruce W. Perry

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

date

Allowed coercions

list with one item
string

Syntax

set dateVar to date "January 1, 2000"

Description

You can use strings to express several different forms of date expressions. But AppleScript will not store the value as a date object unless you precede the string with the date keyword:

set theDate to date "1/1/2000"

It is very easy to forget to include the date keyword; if you leave it out, the variable will be set to a string and will not contain any of the date object’s properties (e.g., Time String).

This example uses the current date scripting addition to return a date object. The script shows the code’s return value within comment characters (“(* *)”):

set theDate to current date
(*date "Friday, November 12, 1999 8:22:11 AM" *)

Once you have a valid date object, then you can obtain the values of several properties from it:

Class

date

Date String

The date not including the time value (“Friday, November 12, 1999”)

Day

An integer that represents the day of the month, as in 12 for “November 12”

Month

Represents one of the following constants:

January
July
February
August
March
September
April
October
May
November
June
December
Time

An integer representing the number of seconds since midnight

Time String

Gets the time from the date object in string form (“8:22:11 AM”)

Weekday

Stored in one of the following constants:

Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday
Year

An integer representing the year

If you create a date object from a literal string, then ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required