• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Conversion of ISO Date String to AppleScript Date
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Conversion of ISO Date String to AppleScript Date


  • Subject: Re: Conversion of ISO Date String to AppleScript Date
  • From: Shane Stanley <email@hidden>
  • Date: Mon, 06 Mar 2017 13:21:04 +1100

It seems to me that there are two issues here. One is how to parse an ISO 8601-style string of a fixed format, for which there are several options. The other is to be able to handle a string that may conform to ISO 8601, but whose format you can't guarantee. That's what Jim is attempting to do, but for a small range of alternatives. 

For a broader range of formats, I don't think AppleScript, NSDateFormatter, or Hamish's library are well suited. As it happens, maOS 10.12 has introduced a new class, NSISO8601DateFormatter, and ironically it's even less suited to the task.

But there's a third-party open-source alternative that fits the bill nicely. You can read about it and download it here: <https://github.com/boredzo/iso-8601-date-formatter>. It handles most formats, including week date and ordinal date formats. That download is not going to be much use to most scripters, but essentially it can compile into a framework that can be called from AppleScript. I've compiled the framework, and you can download it here: <https://www.macosxautomation.com/applescript/apps/ISO8601_framework.zip>.

Once you add it to your system, either in ~/Library/Frameworks or within a library or other script bundle, calling it is simple. You can make it run in a strict mode, or the default of more lenient parsing, where it handles different delimiters and things like leading spaces. Here's an example script:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "ISO8601"
use scripting additions

my ISO8601DateFrom:"2016-01-05 13:01:15"
my ISO8601DateFrom:"2016-01-05"
my ISO8601DateFrom:"2016-01-05T13:01:15"
my ISO8601DateFrom:"2016-01-05 13:01"
my ISO8601DateFrom:"2016-01-05T13:01:15Z"
my ISO8601DateFrom:"2016-01-05 13:01:15+1100"
my ISO8601DateFrom:"2007-W01-01"
my ISO8601DateFrom:"2007-W01-01T13:24:56-0800"
my ISO8601DateFrom:"-01-05"
my ISO8601DateFrom:"2017-032"

on ISO8601DateFrom:theString
set theFormatter to current application's ISO8601DateFormatter's new()
set theDate to theFormatter's dateFromString:theString
considering numeric strings
if AppleScript's version < "2.5" then
set theDate to my makeASDateFrom:theDate
else
set theDate to theDate as date
end if
end considering
return theDate -- AppleScript Date
end ISO8601DateFrom:

# Required before 10.11
on makeASDateFrom:theNSDate
set theCalendar to current application's NSCalendar's currentCalendar()
set comps to theCalendar's componentsInTimeZone:(missing value) fromDate:theNSDate -- 'missing value' means current time zone
tell (current date) to set {theASDate, year, day, its month, day, time} to ¬
{it, comps's |year|(), 1, comps's |month|(), comps's |day|(), (comps's hour()) * hours + (comps's minute()) * minutes + (comps's |second|())}
return theASDate
end makeASDateFrom:

There are also methods for setting the time zone, as well as making strings from dates.

-- 
Shane Stanley <email@hidden>
<www.macosxautomation.com/applescript/apps/>, <latenightsw.com>


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: Conversion of ISO Date String to AppleScript Date
      • From: Yvan KOENIG <email@hidden>
    • Re: Conversion of ISO Date String to AppleScript Date
      • From: Jim Underwood <email@hidden>
References: 
 >Re: Conversion of ISO Date String to AppleScript Date (From: has <email@hidden>)
 >Re: Conversion of ISO Date String to AppleScript Date (From: "Stockly, Ed" <email@hidden>)
 >Re: Conversion of ISO Date String to AppleScript Date (From: has <email@hidden>)
 >Re: Conversion of ISO Date String to AppleScript Date (From: Shane Stanley <email@hidden>)
 >Re: Conversion of ISO Date String to AppleScript Date (From: Jim Underwood <email@hidden>)

  • Prev by Date: Re: Conversion of ISO Date String to AppleScript Date
  • Next by Date: Re: Conversion of ISO Date String to AppleScript Date
  • Previous by thread: Re: Conversion of ISO Date String to AppleScript Date
  • Next by thread: Re: Conversion of ISO Date String to AppleScript Date
  • Index(es):
    • Date
    • Thread