Applescript : String to Date I'm extracting different date formats from a web page.

E.g. Sunday 10 September 2017 / 09/10/2017 /  September 10th, 2017.


  in the example the date is the same, but in live, the dates are
  completely random and different. This could be  01/2/1985 /  November
  10th, 2005


The first one doesn't matter too much as this is the current date, which I'm generating myself and I can change it:

tell (current date)
    set strMonth to (its month as integer)
    set strDay to (its day as integer)
    set stryear to (its year as integer)
end tell

set myOwnDate to strMonth &amp; "/" &amp; strDay &amp; "/" &amp; stryear as string


The format I want is integer day/month/year so 10/09/2017.

Can I change the two other strings (09/10/2017 /  September 10th, 2017) to this format?
You can use the NSDataDetector (a cocoa method) for parsing date from a string.

Here's the script (tested on macOS Sierra):

Warning: on a string which contains "digit/digit/year", the script does not work with a string in the  "day/month/year" format, the string must be in the  "month/day/year" format

use framework "Foundation"
use scripting additions

(* this script works on these formats: 
"Sunday 10th September 2017", "Sunday 10 September 2017", "Sunday 10 September 17", "September 10th, 2017", "September 10th 2017", "September 10 2017"
 "10th September 2017", "10 September 2017", "10 Sep 17"
 also work with the abbreviation ( e.g. Sep instead of September and Sun instead of Sunday) 
 also work with the localized name of (the month and the day)

"09/10/2017", "09.10.2017", "09-10-2017" : month_day_year only,  or "2016/05/22", "2016-05-22", "2016.05.22" : year_month_day only 
( month and days could be one or two digits)
*)

set myString to "Nov 5th, 2005"
set dateString to my stringToDate(myString) --&gt; "05/11/2005"

on stringToDate(thisString) -- 
    tell current application
        -- **  finds all the matches for a date, the result is a NSArray ** 
        set m to its ((NSDataDetector's dataDetectorWithTypes:(its NSTextCheckingTypeDate) |error|:(missing value))'s matchesInString:thisString options:0 range:{0, length of thisString})
        if (count m) &gt; 0 then
            set d to (item 1 of m)'s |date|() -- get the NSDate of the first item 
            set df to its NSDateFormatter's new() -- create a NSDateFormatter 
            df's setDateFormat:"dd/MM/yyyy" -- a specified output format: "day/month/year" (day and month = two digits, year = 4 digits)
            return (df's stringFromDate:d) as text
        end if
    end tell
    return "" -- no match in this string
end stringToDate