• 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: Christopher Stone <email@hidden>
  • Date: Sat, 04 Mar 2017 15:05:45 -0600

On Mar 03, 2017, at 23:08, Jim Underwood <email@hidden> wrote:
The point is how to convert an ISO date string to AppleScript date object.


Hey Jim,

These fall into the category of hack, but they're still working fine and have the advantage of being terse.

------------------------------------------------------------------------------

# Create ISO Date String from an AppleScript Date-Object
set myIsoDateStr to ((current date) as «class isot» as string)

--> "2017-03-04T14:36:22"

------------------------------------------------------------------------------

# Create an AppleScript Date-Object from an ISO Date String.
set isoDateStr to "2013-04-29T21:22"
set myAsDateFromIsoStr to (do shell script "echo " & isoDateStr as «class isot») as date

--> date "Monday, April 29, 2013 at 21:22:00"

------------------------------------------------------------------------------

If you want to seamlessly handle a wider variety of date string formats then AppleScriptObjC and Data-Detectors is probably the way to go.

-------------------------------------------------------------------------------------------
# Auth: Christopher Stone <email@hidden>
# Auth: Shane Stanley graciously provided the ASObjC handlers.
# dCre: 2016/03/17 03:45
# dMod: 2016/03/17 04:09
# Appl: AppleScriptObjC
# Task: Use Data-Detectors to extract a date from unstructured string input.
#     : Output an ISO 8601 formatted date-string.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @ASObjC, @ISO, @Date, @String
-------------------------------------------------------------------------------------------
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
-------------------------------------------------------------------------------------------

# Example Date String Inputs
# set theDate to my getDatesIn:"Some text containing Nov 5, for example"
# set theDate to my getDatesIn:"2015/1/5"
set theDate to my getDatesIn:"2017-03-04 14:58:39"
# set theDate to my getDatesIn:"1/5/2016"
# set theDate to my getDatesIn:"4 May 1961"
# set theDate to my getDatesIn:"jan 1 2012"

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

# Date String Output
set outputDate to my formatDate:(theDate) usingFormat:"y-MM-dd"
return outputDate

-------------------------------------------------------------------------------------------
--» HANDLERS
-------------------------------------------------------------------------------------------
on formatDate:theDate usingFormat:formatString
if class of theDate is date then set theDate to my makeNSDateFrom:theDate
set theFormatter to current application's NSDateFormatter's new()
theFormatter's setLocale:(current application's NSLocale's localeWithLocaleIdentifier:"en_US_POSIX")
theFormatter's setDateFormat:formatString
set theString to theFormatter's stringFromDate:theDate
return theString as text
end formatDate:usingFormat:
-------------------------------------------------------------------------------------------
on getDatesIn:aString
# Convert string to Cocoa string
set anNSString to current application's NSString's stringWithString:aString
# Create data detector
set theDetector to current application's NSDataDetector's dataDetectorWithTypes:(current application's NSTextCheckingTypeDate) |error|:(missing value)
# Find first match in string; returns an NSTextCheckingResult object
set theMatch to theDetector's firstMatchInString:anNSString options:0 range:{0, anNSString's |length|()}
if theMatch = missing value then error "No date found"
# Get the date property of the NSTextCheckingResult
set theDate to theMatch's |date|()
return theDate
end getDatesIn:
-------------------------------------------------------------------------------------------
# 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:
-------------------------------------------------------------------------------------------
on makeNSDateFrom:theASDate
set {theYear, theMonth, theDay, theSeconds} to theASDate's {year, month, day, time}
if theYear < 0 then
set theYear to -theYear
set theEra to 0
else
set theEra to 1
end if
set theCalendar to current application's NSCalendar's currentCalendar()
set newDate to theCalendar's dateWithEra:theEra |year|:theYear |month|:(theMonth as integer) ¬
|day|:theDay hour:0 minute:0 |second|:theSeconds nanosecond:0
return newDate
end makeNSDateFrom:
-------------------------------------------------------------------------------------------

--
Take Care,
Chris

 _______________________________________________
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: Shane Stanley <email@hidden>
References: 
 >Conversion of ISO Date String to AppleScript Date (From: Jim Underwood <email@hidden>)

  • Prev by Date: Re: collectdata
  • 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