The FooDoo Lounge

Date String Handlers

Introduction

Some super fast handlers for returning date strings in various formats that will work under any version of AppleScript. Slightly more compact versions are included which work under late model systems only.

Unless otherwise noted, they take an AppleScript date object as their only parameter. See the Date Info page for more on AS dates.

Stand alone handlers

Complete and ready to use. Any other format is possible. Roll your own if what you want isn't here.

The alphanumeric date handlers contain lists of short month names that must be localised for languages other than English, but all handlers will otherwise work regardless of Date & Time settings &/or language. If you're really in a hurry, you can speed these up slightly by declaring the short month list as a property.

ISO Date - yyyy-mm-dd

-- isoDate - yyyy-mm-dd
-- Garvey/Levy/Morton, 1998-2003
on isoDate from dateObj
    copy dateObj to dateTemp
    set month of dateTemp to January
    return (year of dateObj as string) & "-" & ¬
        text 2 thru 3 of ((100 + ((dateTemp - 2.5E+6 - dateObj) div -2.5E+6)) as string) ¬
        & "-" & text 2 thru 3 of ((100 + (dateObj's day)) as string)
end isoDate

For AS 1.9.2 (OS 10.3) or later only:

-- isoDate - yyyy-mm-dd - for AS 1.9.2 or higher
-- Richard Morton & Nigel Garvey 2004
on isoDate from dateObj
    tell dateObj to return (its year as string) & "-" & ¬
        text 2 thru 3 of ((100 + (its month as integer)) as string) ¬
        & "-" & text 2 thru 3 of ((100 + (its day)) as string)
end isoDate

Short Alphanumeric Date - ddMonyyyy

-- shortAnDate - ddMonyyyy
-- Garvey/Levy/Morton, 1998-2003
to shortAnDate from dateObj
    copy dateObj to dateTemp
    set {sMonList, month of dateTemp} to ¬
        {{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", ¬
            "Sep", "Oct", "Nov", "Dec"}, January}
    return text 2 thru 3 of ((100 + (dateObj's day)) as string) & ¬
        sMonList's item ((dateTemp - 2.5E+6 - dateObj) div -2.5E+6) & year of dateObj
end shortAnDate

For AS 1.3.7 (OS 8.6) or later only:

-- shortAnDate - ddMonyyyy - for AS 1.3.7 or higher
-- Richard Morton & Nigel Garvey 2004
to shortAnDate from dateObj
    tell dateObj to return text 2 thru 3 of ((100 + (its day)) as string) & ¬
        text 1 thru 3 of (its month as string) & its year
end shortAnDate

Short Alphanumeric Date - yyyyMondd

-- shortAnDate - yyyyMondd
-- Garvey/Levy/Morton, 1998-2003
to shortAnDate from dateObj
    copy dateObj to dateTemp
    set {sMonList, month of dateTemp} to ¬
        {{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", ¬
            "Sep", "Oct", "Nov", "Dec"}, January}
    return ((year of dateObj) as string) & ¬
        sMonList's item ((dateTemp - 2.5E+6 - dateObj) div -2.5E+6) & ¬
        text 2 thru 3 of ((100 + (dateObj's day)) as string)
end shortAnDate

For AS 1.3.7 (OS 8.6) or later only:

-- shortAnDate - yyyyMondd - for AS 1.3.7 or higher
-- Richard Morton & Nigel Garvey 2004
to shortAnDate from dateObj
    tell dateObj to return (its year as string) & text 1 thru 3 of ¬
        (its month as string) & text 2 thru 3 of ((100 + (its day)) as string)
end shortAnDate

Short numeric date in AU/UK format - dd-mm-yyyy

-- numDateAU - dd-mm-yyyy
-- Garvey/Levy/Morton, 1998-2003
on numDateAU from dateObj
    copy dateObj to dateTemp
    set month of dateTemp to January
    return text 2 thru 3 of ((100 + (dateObj's day)) as string) & "-" & ¬
        text 2 thru 3 of ((100 + ((dateTemp - 2.5E+6 - dateObj) div -2.5E+6)) as string) ¬
        & "-" & year of dateObj
end numDateAU

For AS 1.9.2 (OS 10.3) or later only:

-- numDateAU - dd-mm-yyyy - for AS 1.9.2 or higher
-- Richard Morton & Nigel Garvey 2004
on numDateAU from dateObj
    tell dateObj to return text 2 thru 3 of ((100 + (its day)) as string) & "-" & ¬
        text 2 thru 3 of ((100 + (its month as integer)) as string) & "-" & its year
end numDateAU

Short numeric date in US format - mm-dd-yyyy

-- numDateUS - mm-dd-yyyy
-- Garvey/Levy/Morton, 1998-2003
on numDateUS from dateObj
    copy dateObj to dateTemp
    set month of dateTemp to January
    return text 2 thru 3 of ((100 + ((dateTemp - 2.5E+6 - dateObj) div -2.5E+6)) as string) ¬
        & "-" & text 2 thru 3 of ((100 + (dateObj's day)) as string) & "-" & year of dateObj
end numDateUS

For AS 1.9.2 (OS 10.3) or later only:

-- numDateUS - mm-dd-yyyy - for AS 1.9.2 or higher
-- Richard Morton & Nigel Garvey 2004
on numDateUS from dateObj
    tell dateObj to return text 2 thru 3 of ((100 + (its month as integer)) as string) ¬
        & "-" & text 2 thru 3 of ((100 + (its day)) as string) & "-" & its year
end numDateUS

Component handlers

These return date string pieces and are for use where a script or library contains more than one date handler.

Get a month number

-- monthNum - French Vanilla
-- Return the month number from a date object
-- Hartman's tweak of Levy's method
on monthNum from dateObj
    copy dateObj to dateTemp
    set month of dateTemp to January
    return (1 + (dateObj - dateTemp) div 2.5E+6)
end monthNum

Get a file name friendly time string

-- timeString -- by Richard Morton 2000
-- Return a file name friendly time string from a date object
on timeString from dateObj
    set timeStr to time string of dateObj
    tell AppleScript -- Just saves typing
        set oD to text item delimiters
        set text item delimiters to ":" -- assumes colon time separator
        set timeStrItems to text items of timeStr
        set text item delimiters to "-" -- change to something else if req.
        set newTimeStr to timeStrItems as string
        set text item delimiters to oD
        return newTimeStr
    end tell
end timeString

 

The FooDoo Lounge is Copyright © Richard Morton 2002-2005

|| url: http://www.foodoo.sunreal.com.au/code/date_handlers.html
|| created: 4-Aug-03, 10:11 PM; updated: 4-Sep-04, 11:33 PM
|| size: 58279 bytes