Skip to content

Instantly share code, notes, and snippets.

@NaanProphet
Last active December 24, 2015 22:11
Show Gist options
  • Save NaanProphet/ab1154bb94076e04cde5 to your computer and use it in GitHub Desktop.
Save NaanProphet/ab1154bb94076e04cde5 to your computer and use it in GitHub Desktop.
A helpful script to convert dates generated by IFTTT receipes like those from Maker into the ISO standard (that's used e.g. by MSQL).
-- IFTTT convert date function. Call with string in format generated by
-- receipes, e.g. "December 24, 2015 at 05:25AM"
on toISOformat(iftttDate)
set theDate to date (iftttDate)
-- inspired by: http://henrysmac.org/blog/2014/1/4/formatting-short-dates-in-applescript.html
set y to text -4 thru -1 of ("0000" & (year of theDate))
set m to text -2 thru -1 of ("00" & ((month of theDate) as integer))
set d to text -2 thru -1 of ("00" & (day of theDate))
-- special thanks to: http://alvinalexander.com/blog/post/mac-os-x/applescript-getting-current-time
set secFromMidnight to (time of theDate)
set h to text -2 thru -1 of ("00" & (secFromMidnight / 3600 as integer))
set min to text -2 thru -1 of ("00" & ((secFromMidnight - 3600 * h) / 60 as integer))
set s to text -2 thru -1 of ("00" & ((secFromMidnight - 3600 * h - 60 * min) as integer))
return y & "-" & m & "-" & d & " " & h & ":" & min & ":" & s
end toISOformat
-- Demo
set iftttDate to "December 24, 2015 at 05:25AM"
set dateString to toISOformat(iftttDate)
--"2015-12-24 05:25:00"
@NaanProphet
Copy link
Author

To convert from an ISO string into an almost-IFTTT date string (IFTTT does not put a space before AM or PM), see the following gist, copied below for convenience.

link https://gist.github.com/RichardHyde/3386ac57b55455b71140

-- Convert date function. Call with string in YYYY-MM-DD HH:MM:SS format (time part optional)
to convertDate(textDate)
    set resultDate to the current date

    set the year of resultDate to (text 1 thru 4 of textDate)
    set the month of resultDate to (text 6 thru 7 of textDate)
    set the day of resultDate to (text 9 thru 10 of textDate)
    set the time of resultDate to 0

    if (length of textDate) > 10 then
        set the hours of resultDate to (text 12 thru 13 of textDate)
        set the minutes of resultDate to (text 15 thru 16 of textDate)

        if (length of textDate) > 16 then
            set the seconds of resultDate to (text 18 thru 19 of textDate)
        end if
    end if

    return resultDate
end convertDate

-- Demo
set isoDate to "2016-12-12 11:11:12"
set almostIftttDate to convertDate(isoDate)
--date "Monday, December 12, 2016 at 11:11:12 AM"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment