• 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: Jim Underwood <email@hidden>
  • Date: Sun, 05 Mar 2017 06:57:33 +0000
  • Thread-topic: Conversion of ISO Date String to AppleScript Date

Shane, thanks for this great tip.
I have updated my standard date handler accordingly.

Hey guys, please try to shoot holes in this script.
I didn't use stringToList because I don't care about the delimiters, only the position of the actual date/time data.
Plus, this allows more flexibility in decoding the date/time string.
I'm sure the code could be more compact, but I'm more interested in readability than compactness.



use AppleScript version "2.4" -- Yosemite (10.10) or later

use scripting additions


--- ENTER THE DATE IN THE FORMAT THAT IS SET IN YOUR SYSTEM PREFERENCES ---

set dateInYourLocale to convertToDate("02/01/2017")


--- INTERNATIONAL DATE FORMAT ---

set dateISO to convertToDate("2017-01-02")


log ("dateInYourLocale: " & dateInYourLocale as text)

log ("dateISO: " & dateISO as text)


--- NOW TRY INVALID DATE FORMAT FOR U.S. ---

--    (but should work for Australia)

set dateAus to convertToDate("31/01/2017")

--> error



### RESULTS ###

(*dateInYourLocale: Wed, Feb 1, 2017 at 12:00:00 AM*)

(*dateISO: Mon, Jan 2, 2017 at 12:00:00 AM*)

(*

[ERROR]

INVALID Date Format: 31/01/2017

*)


--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--    HANDLERS (functions)

--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

on convertToDate(pDate) -- @Date @Convert @String

  (*  VER: 1.1    2017-03-02

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

    PURPOSE:  Convert Date String to AppleScript Date Type

    PARAMETERS:

      • pDate    | text OR date  | Date to check/convert

    RETURNS:  date │  AppleScript date    

    AUTHOR:  JMichaelTX

    —————————————————————————————————————————————————————————————————————————————————

  *)

  

  -- MUST NOT BE IN ANY APP WHEN USING THE date FUNCTION --

  

  if (class of pDate = date) then

    set dateAsDate to pDate

    

  else -- Convert date string

    

    try -- Try Standard date format as set in System Preferences --

      set dateAsDate to date (pDate)

    on error --- Try International Date format (YYYY-MM-DD)

      set dateAsDate to my convertISOtoDate(pDate)

    end try

    

  end if

  

  return dateAsDate

  

end convertToDate

--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

on convertISOtoDate(pDateStr) -- @Date @ISO @String

  (*  VER: 1.2    2017-03-03

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

  PURPOSE:  Convert Date (w/optional time) String in ISO format to AppleScript Date Type

  PARAMETERS:

    • pDateStr    | text | Date String in ISO format 

                                 Assumes local time zone

                                 If time is included, assumed to be 24-HR format

  RETURNS:  date │  AppleScript date    

  AUTHOR:  JMichaelTX

  

  pDateStr MUST be in the format of 

    YYYY<delim>MM<delim>DD

      OR, if time with minutes is included:

    YYYY<delim>MM<delim>DD<AnyChar>HH<delim>MM

      OR, if time with seconds is included:

    YYYY<delim>MM<delim>DD<AnyChar>HH<delim>MM<delim>SS

    in local time.

    where <delim> can be any character

    like any of these:

      2016-01-05

      2016-01-05 13:01:15

      2016-01-05T13:01:15

      2016-01-05 13:01

      2016-01-05 13:01:15

    —————————————————————————————————————————————————————————————————————————————————

*)

  

  

  set resultDate to the current date

  set the day of resultDate to 1 ## Thanks to Shane

  

  try

    --- ANY/ALL DELIMITERS OF 1 CHAR WILL WORK ---

    set the year of resultDate to (text 1 thru 4 of pDateStr) as integer

    set the month of resultDate to (text 6 thru 7 of pDateStr) as integer

    set the day of resultDate to (text 9 thru 10 of pDateStr) as integer

    set the time of resultDate to 0

    

    if (length of pDateStr) > 10 then -- include HH:MM

      set the hours of resultDate to (text 12 thru 13 of pDateStr) as integer

      set the minutes of resultDate to (text 15 thru 16 of pDateStr) as integer

      

      if (length of pDateStr) > 16 then -- include seconds

        set the seconds of resultDate to (text 18 thru 19 of pDateStr) as integer

      end if

    end if

    

  on error errMsg number errNum

    error "[ERROR]" & return & "INVALID Date Format: " & pDateStr

    

  end try

  

  return resultDate

  

end convertISOtoDate

--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~





Best Regards,

 

Jim Underwood

aka JMichaelTX


From: <applescript-users-bounces+jmichael=email@hidden> on behalf of Shane Stanley <email@hidden>
Date: Sat, Mar 4, 2017 at 11:58 PM
To: "ASUL (AppleScript)" <email@hidden>
Subject: Re: Conversion of ISO Date String to AppleScript Date

When you set the month to 2, there aren't 31 days in February, so it it rolls the date over to the next month by however many days there are in excess. The easiest solution is to set the day to 1 (or any number less than 29) before setting the month. So this change to your script:
 _______________________________________________
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: 
 >Re: Conversion of ISO Date String to AppleScript Date (From: Ed Stockly <email@hidden>)
 >Re: Conversion of ISO Date String to AppleScript Date (From: Ed Stockly <email@hidden>)
 >Re: Conversion of ISO Date String to AppleScript Date (From: Ed Stockly <email@hidden>)
 >Re: Conversion of ISO Date String to AppleScript Date (From: Ed Stockly <email@hidden>)
 >Re: Conversion of ISO Date String to AppleScript Date (From: Shane Stanley <email@hidden>)

  • Prev by Date: Re: how to create a reference a variable at runtime
  • Next by Date: Contacts.app metadata associated with each entry
  • 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