Converting dates to and from ISO 8601 format strings – and more specifically RFC 3339 format strings – is easily done using a date formatter, but there is a catch.
Here’s a pair of handlers:
use framework "Foundation"
use scripting additions
set theDate to current application's NSDate's |date|() -- now
set dateString to my rfc3339FromDate:theDate
--> "2017-11-27T06:38:31Z"
set theDate to (my dateFromRfc3339String:dateString) as date
on rfc3339FromDate:aDate
set theFormatter to current application's NSDateFormatter's new()
theFormatter's setLocale:(current application's NSLocale's localeWithLocaleIdentifier:"en_US_POSIX")
theFormatter's setTimeZone:(current application's NSTimeZone's timeZoneWithAbbreviation:"GMT") -- skip for local time
theFormatter's setDateFormat:"yyyy'-'MM'-'dd'T'HH':'mm':'ssXXX"
return (theFormatter's stringFromDate:aDate) as text
end rfc3339FromDate:
on dateFromRfc3339String:theString
set theFormatter to current application's NSDateFormatter's new()
theFormatter's setLocale:(current application's NSLocale's localeWithLocaleIdentifier:"en_US_POSIX")
theFormatter's setTimeZone:(current application's NSTimeZone's timeZoneWithAbbreviation:"GMT") -- skip for local time
theFormatter's setDateFormat:"yyyy'-'MM'-'dd'T'HH':'mm':'ssXXX"
return theFormatter's dateFromString:theString
end dateFromRfc3339String:
The catch: the handlers deal in NSDates. Under 10.11 and later, you can pass AppleScript dates, and coerce the result to AppleScript dates, but under earlier versions you need to add code to do the conversion.
Note that setting the locale to en_US_POSIX matters, otherwise the date format string might be interpreted differently depending on your locale.
See https://www.unicode.org/reports/tr35/tr35-dates.html#Date_Field_Symbol_Table for details of the various characters’ meaning in a format string.
You can of course use similar methods to format date strings however you wish.