आप स्ट्रिंग से पार्सिंग दिनांक के लिए NSDataDetector ( एक कोको विधि ) का उपयोग कर सकते हैं ।
यहाँ स्क्रिप्ट ( macOS सिएरा पर परीक्षण ) है:
चेतावनी: एक स्ट्रिंग पर जिसमें " अंक / अंक / वर्ष " होता है, स्क्रिप्ट " दिन / महीने / वर्ष " प्रारूप में स्ट्रिंग के साथ काम नहीं करती है , स्ट्रिंग को " महीने / दिन / वर्ष " प्रारूप में होना चाहिए
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