Doing Time Offsets of HH/MM/SS and getting the MM/DD/YYYY/HH/MM/SS right

Yes, but you cannot seem to do the reverse of that.

Apparently in older versions of the system, you could do something like:

date ("2017-03-04T11:15:01" as «class isot»)

But that no longer works.

Without the “T” it works:

date ("2017-03-04 11:15:01")

date "Saturday, 4 March 2017 at 11:15:01"

So, from localized current date → ISO date → localized date:

set AppleScript's text item delimiters to {"T"}
set isoDate to text items of ((current date) as «class isot» as string)
set AppleScript's text item delimiters to {" "}
set localizedDate to date (isoDate as text)

Hmmm, that doesn't work for me:

Running Script Editor 2.8.1 (183.1) on macOS 10.11.6


That doesn't work either, this time in Script Debugger:

Script Debugger 6.0.4 (6A198) on macOS 10.11.6.

@Tom, it must be something in your system that allows that?

1 Like

Hmm. Works in both ScriptDebugger 6.0.4 (6A198) and in Script Editor 2.9 (191); macOS 10.12.3 (16D32).

Removed all .osax from /Library/ScriptingAdditions and ~/Library/ScriptingAdditions, restarted the Mac, and it still works.

Tom, I just tried it on a different Mac, but with same result, same error.

So, it must be how AppleScript is handling localization of dates -- that's all I can think of.

I'll post on the AppleScript Users List, and see what people report.

Yep, on my other Mac (also 10.12.3) I get the same error as you.

Have to see what this is…

@Tom, this just reported from the ASUL:

Works fine for me on 10.12.3:

date "Saturday, March 4, 2017 at 11:15:01"

My short date pref is set to yyyy/MM/dd instead of the standard MM/dd/yyyy, if that makes any difference.

OK, found it: it works on the one Mac because the system’s short date format was set to ISO:

3 Likes

Yes, this makes the difference.

Bingo!

Heck, stupid AppleScript!

1 Like

So you can use this handler instead:

set myDate to my convertISOtoDate("2017-03-04T11:15:01")

on convertISOtoDate(pDateStr)
  
  --- pDateStr MUST be in the format of 
  --- YYYY<delim>MM<delim>DD
  --    OR
  --- YYYY<delim>MM<delim>DD<delim>HH:dd:ss
  
  ---    where <delim> can be any character
  ---    like 2016-01-05
  
  set resultDate to the current date
  
  set the year of resultDate to (text 1 thru 4 of pDateStr)
  set the month of resultDate to (text 6 thru 7 of pDateStr)
  set the day of resultDate to (text 9 thru 10 of pDateStr)
  set the time of resultDate to 0
  
  if (length of pDateStr) > 10 then
    set the hours of resultDate to (text 12 thru 13 of pDateStr)
    set the minutes of resultDate to (text 15 thru 16 of pDateStr)
    
    if (length of pDateStr) > 16 then
      set the seconds of resultDate to (text 18 thru 19 of pDateStr)
    end if
  end if
  
  return resultDate
  
end convertISOtoDate

-->date "Saturday, March 4, 2017 at 11:15:01 AM"