How to resolve the algorithm Date manipulation step by step in the AppleScript programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Date manipulation step by step in the AppleScript programming language

Table of Contents

Problem Statement

Given the date string "March 7 2009 7:30pm EST", output the time 12 hours later in any human-readable format. As extra credit, display the resulting time in a time zone different from your own.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Date manipulation step by step in the AppleScript programming language

Source code in the applescript programming language

set x to "March 7 2009 7:30pm EST"
return (date x) + 12 * hours


date "Sunday, March 8, 2009 7:30:00 AM"


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()


  

You may also check:How to resolve the algorithm Weird numbers step by step in the Crystal programming language
You may also check:How to resolve the algorithm Natural sorting step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Shoelace formula for polygonal area step by step in the jq programming language
You may also check:How to resolve the algorithm Keyboard input/Keypress check step by step in the Julia programming language
You may also check:How to resolve the algorithm Host introspection step by step in the Scala programming language