2

I am building a script that saves many consecutive screenshots. I would like to save the files in the ISO 8601 date format.

3

To get the date in ISO 8601 format, use:

on date_to_iso(dt)
    set {year:y, month:m, day:d} to dt
    set y to text 2 through -1 of ((y + 10000) as text)
    set m to text 2 through -1 of ((m + 100) as text)
    set d to text 2 through -1 of ((d + 100) as text)
    return y & "-" & m & "-" & d
end date_to_iso

on date_time_to_iso(dt)
    set {year:y, month:m, day:d, hours:h, minutes:min, seconds:s} to dt
    set y to text 2 through -1 of ((y + 10000) as text)
    set m to text 2 through -1 of ((m + 100) as text)
    set d to text 2 through -1 of ((d + 100) as text)
    set h to text 2 through -1 of ((h + 100) as text)
    set min to text 2 through -1 of ((min + 100) as text)
    set s to text 2 through -1 of ((s + 100) as text)
    return y & "-" & m & "-" & d & "T" & h & ":" & min & ":" & s
end date_time_to_iso

You can call these with, for example:

set dt to current date
date_to_iso(dt)

gives something like "2013-10-23"

set dt to current date
date_time_to_iso(dt)

gives something like "2013-10-23T14:00:05"

Source: AppleScript Date to ISO Format

1

There isn't any single ISO 8601 date format: for example 20130823, 2013W43, and 2013-123 are valid ISO 8601 dates.

If you mean the format like 2013-10-23T21:03:34, colons are not valid characters in filenames in Finder or other GUI applications. Colons in the shell are displayed as slashes in Finder and vice versa.

This would use a format like 20131023210334:

do shell script "screencapture ~/Desktop/$(date +%Y%y%m%d%H%M%S).png"

date -u +%FT%TZ (where -u sets the time zone to UTC) would use a format like 2013-10-23T18:03:34Z, but the colons would be displayed as slashes in GUI applications.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .