KevinCork KevinCork - 3 months ago 18
AppleScript Question

AppleScript reformat date

One of my variable is a date in this format "August 7th, 2016"

My system date is DD/MM/YY

how can I change the AppleScript date, and then calculate how many day from today this was?

Kind regards

Edit (date convertion)

set X to set {year:y, month:m, day:d} to (current date)

set convertedTime to m & d & y


or

set {year:y, month:m, day:d} to (current date)

set convertedTime to m & " " & d & "th," & " " & y


Update :

Whaever I do, I have Invalid date and time date june 3, 2006. Would that be because 10.11 updated applescript?

tell (date "june 3, 2006") to get its month as integer

Answer

if really your date "August 7th, 2016" does not match at all your regional date settings, the script bellow is parsing that date to convert each term. Of course, it would be much more efficient to match regional date setting !

-- date conversion without taking care of regional settings
-- assume date in in English format and returned format will be dd/mm/yy (not mm/dd/yy)
set X to "August 7th, 2016"

set D to ConvertDate(X)
log D


on ConvertDate(X) -- sub routine to convert string "english_month dayth/st, year" to real date
set MS to {"January", February, "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}
set LW to every word of X
if (count of LW) is not 3 then return "" -- invalid format
set MI to 0 -- check month : should be in the list
repeat with I from 1 to 12
    if item I of MS is item 1 of LW then set MI to I
end repeat
if MI is 0 then return "" -- the fisrt word is not in the list of months    
try -- check day : it should be NNth of NNst
    set DI to (text 1 thru -3 of item 2 of LW) as integer
end try
if not ((DI > 0) and (DI < 31)) then return "" -- invalid day
try -- check year
    set YI to (item 3 of LW) as integer
end try
if not ((YI > 0) and (YI < 9999)) then return "" -- invalid year
return date ((DI & "/" & MI & "/" & YI) as string)
end ConvertDate
Comments