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 & "/" & strDay & "/" & 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?

  • I found this gist. You should be able to modify this to fit your needs.
    – Joonas
    Sep 10 '17 at 13:21
  • In your question: you want day/month/year, but your script returns a month/day/year string.
    – jackjr300
    Sep 11 '17 at 2:19

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) --> "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) > 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
  • 2
    I could be wrong, however, I see an issue here. If, for example, the extracted date from the webpage is a string in the format of 09/10/2016, could that be either September 10 2016 or October 09 2016 depending on the US which uses MDY or many other places that use DMY, and by itself without other context I do not believe there is a way to determine which one it actually is. Therefore, if I pass 09/10/2016 to your handler, it returns "10/09/2016", so, could you please explain how one knows whether or not this string without any other reference, 09/10/2016, started as MDY or DMY? Sep 10 '17 at 23:17
  • 1
    Right, It's impossible to know for a string of type "digit/digit/year", for this type, my script will assume that it contains a MDY date. No script or code can find it. Only the user will know that on some web page, the dates will be in the D/M/Y format or M/D/Y, so for some page, the script will not need any function since the string (date) is already in the right format
    – jackjr300
    Sep 11 '17 at 2:10

Your script probably won't return a date...

set myOwnDate to date (myOwnDate)

That won't work...

... but you can convert it to a date when you apply it to the property of an item whose format is a date. For example:

    set myOwnDate to "10/09/2017" as string
    set dt to path to desktop
    tell application "Finder"
      set the modification date of (alias (dt & "myFile.txt" as string)) to (date myOwnDate) 
    end tell

This worked for me.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .