• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Convert Text Date to Formatted Date String
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Convert Text Date to Formatted Date String


  • Subject: Convert Text Date to Formatted Date String
  • From: Christopher Stone <email@hidden>
  • Date: Fri, 26 Aug 2016 14:44:54 -0500

Hey Folks,

I just posted this on the Keyboard Maestro Forum and thought I'd drop a copy here as well.

I got sick of having to rewrite dates, so I was looking for a way to take virtually any date format and output to my preferred date format.

Shane suggested using Data-Detectors and provided some nice ASObjC code, and I've been monkeying with it ever since.

Personally I tend to use the ISO 8601 date format, but I wrote this for someone who preferred mm-dd-yyyy.

In any case it gives you the basic pieces to play with.

http://userguide.icu-project.org/formatparse/datetime#TOC-Date-Time-Format-Syntax

--
Best Regards,
Chris

-------------------------------------------------------------------------------------------
# Auth: Christopher Stone { Heavey Lifting by Shane Stanley }
# dCre: 2016/08/26 13:30
# dMod: 2016/08/26 14:02
# Appl: AppleScriptObjC
# Task: Use Data-Detectors to find a date from a string and create a formatted date-string.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Data_Detectors, @Find, @Date, @String, @Create, @Formatted, @DateString
-------------------------------------------------------------------------------------------
use framework "Foundation"
use scripting additions
-------------------------------------------------------------------------------------------

# For reference – today's date is 2016/08/26

set dateStr to "may 4"
set newDateStr to my getMyDateStringFrom:dateStr

--> "05-04-2017"

set dateStr to "4 may 1961"
set newDateStr to my getMyDateStringFrom:dateStr

--> "05-04-1961"

set dateStr to "may 4 1961"
set newDateStr to my getMyDateStringFrom:dateStr

--> "05-04-1961"

set dateStr to "today"
set newDateStr to my getMyDateStringFrom:dateStr

--> "08-26-2016"

set dateStr to "tomorrow"
set newDateStr to my getMyDateStringFrom:dateStr

--> "08-27-2016"

set dateStr to "sunday"
set newDateStr to my getMyDateStringFrom:dateStr

--> "08-28-2016"

set dateStr to "friday"
set newDateStr to my getMyDateStringFrom:dateStr

--> "09-02-2016"

-------------------------------------------------------------------------------------------
--» HANDLERS
-------------------------------------------------------------------------------------------
on formatDate:theDate usingFormat:formatString
if class of theDate is date then set theDate to my makeNSDateFrom:theDate
set theFormatter to current application's NSDateFormatter's new()
theFormatter's setLocale:(current application's NSLocale's localeWithLocaleIdentifier:"en_US_POSIX")
theFormatter's setDateFormat:formatString
set theString to theFormatter's stringFromDate:theDate
return theString as text
end formatDate:usingFormat:
-------------------------------------------------------------------------------------------
on getDatesIn:aString
set anNSString to current application's NSString's stringWithString:aString
set {theDetector, theError} to current application's NSDataDetector's dataDetectorWithTypes:(current application's NSTextCheckingTypeDate) |error|:(reference)
set theMatches to theDetector's matchesInString:anNSString options:0 range:{0, anNSString's |length|()}
set theDates to current application's NSMutableArray's array()
repeat with i from 1 to theMatches's |count|()
set thisMatch to (theMatches's objectAtIndex:(i - 1))
(theDates's addObject:(thisMatch's |date|()))
end repeat


return theDates as list


end getDatesIn:
-------------------------------------------------------------------------------------------
on getMyDateStringFrom:dateStr
set dateList to my getDatesIn:dateStr
if length of dateList = 0 then
error "No dates were returned from the given string!"
else if length of dateList = 1 then
set dateStr to item 1 of dateList
set dateString to my formatDate:dateStr usingFormat:"MM-dd-Y"
else if length of dateList > 1 then
error "Too many dates were found in the given string!"
end if
return dateString
end getMyDateStringFrom:
-------------------------------------------------------------------------------------------
on makeNSDateFrom:theASDate
set {theYear, theMonth, theDay, theSeconds} to theASDate's {year, month, day, time}
if theYear < 0 then
set theYear to -theYear
set theEra to 0
else
set theEra to 1
end if
set theCalendar to current application's NSCalendar's currentCalendar()
set newDate to theCalendar's dateWithEra:theEra |year|:theYear |month|:(theMonth as integer) ¬
|day|:theDay hour:0 minute:0 |second|:theSeconds nanosecond:0
return newDate
end makeNSDateFrom:

-------------------------------------------------------------------------------------------

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Prev by Date: Anyone use Macterm
  • Next by Date: "where its" vs "whose"
  • Previous by thread: Re: Anyone use Macterm
  • Next by thread: "where its" vs "whose"
  • Index(es):
    • Date
    • Thread