Date manipulation: Difference between revisions

→‎{{header|AppleScript}}: Added ASObjC alternative.
(add SenseTalk examples)
(→‎{{header|AppleScript}}: Added ASObjC alternative.)
Line 132:
Result is:
<lang AppleScript>date "Sunday, March 8, 2009 7:30:00 AM"</lang>
 
The above is problematical in that:
# Using a date string in an AppleScript date specifier only works if the date format set in the user's preferences on the host machine is the same as the one used in the string.
# The US clocks went forward in the twelve hours specified by the task and, as already noted, vanilla AppleScript has no way of dealing with that.
 
 
However, AppleScript can run shell scripts and, more recently, access some of the system's Objective-C API through its hybrid form AppleScriptObjectiveC. So as long as the date format's known, the task is doable:
 
<lang applescript>use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
 
on dateManipulationTask()
set dateString to "March 7 2009 7:30pm EST"
set dateFormatter to current application's class "NSDateFormatter"'s new()
tell dateFormatter to setDateFormat:("MMMM d yyyy h:mma z")
tell dateFormatter to setAMSymbol:("am")
tell dateFormatter to setPMSymbol:("pm")
set USLocale to current application's class "NSLocale"'s localeWithLocaleIdentifier:("en_US")
tell dateFormatter to setLocale:(USLocale)
set timeZone to current application's class "NSTimeZone"'s timeZoneWithAbbreviation:(last word of dateString)
tell dateFormatter to setTimeZone:(timeZone)
set inputDate to dateFormatter's dateFromString:(dateString)
set newDate to current application's class "NSDate"'s dateWithTimeInterval:(12 * hours) sinceDate:(inputDate)
return (dateFormatter's stringFromDate:(newDate)) as text
end dateManipulationTask
 
dateManipulationTask()</lang>
 
{{output}}
<pre>"March 8 2009 8:30am EDT"</pre>
 
=={{header|AutoHotkey}}==
557

edits