Skip to content

Instantly share code, notes, and snippets.

@athiyadeviyani
Last active July 23, 2018 08:01
Show Gist options
  • Save athiyadeviyani/787cd4ad51d633f9278d65df0d4f9654 to your computer and use it in GitHub Desktop.
Save athiyadeviyani/787cd4ad51d633f9278d65df0d4f9654 to your computer and use it in GitHub Desktop.
an applescript that performs simple date calculations
-- Performs the following date calculations:
-- What is the date [input days] ago?
-- What is the date [input days] later?
-- How many days ago is the [input date]?
-- How many days until the [input date]?
repeat
-- main switchboard
display dialog "Welcome to DAYS COUNTER! Choose the calculation:" buttons {"The date x days ago/later", "How many days to/since", "Exit"}
set btn to button returned of result
-- COUNT THE DATE [INPUT] DAYS AGO
if btn is "The date x days ago/later" then
set myDate to current date
display dialog "What is the date x days ago (-) or days later (+)?
Use a minus sign (-) for days ago." default answer "0"
set btn to button returned of result
set theDays to text returned of result
set myNewDate to myDate + (theDays * days)
set myNewDate to date string of myNewDate
display dialog myNewDate
-- EXITS THE APPLICATION
-- probably not a good idea to put under else if, but works either way
else if btn is "Exit" then
return
-- COUNT HOW MANY DAYS TO/SINCE THE INPUT DATE
else
set myDate to current date
set theDate to text returned of (display dialog "Please enter a starting/end date:" default answer "3 May 2018 12:00PM")
set myNewDate to date theDate
if myNewDate is greater than myDate then
set theResult to myNewDate - myDate
set theDay to round (theResult / 86400)
set theWeek to round theDay / 7
set theMonth to round theDay / 30
display dialog (myNewDate as string) & " is" & return & (((theDay as string) & " days OR " & return & theWeek as string) & " weeks OR " & return & theMonth as string) & " months" & return & "LATER"
else
set theResult to myDate - myNewDate
set theDay to round (theResult / 86400)
set theWeek to round theDay / 7
set theMonth to round theDay / 30
display dialog (myNewDate as string) & " is" & return & (((theDay as string) & " days OR " & return & theWeek as string) & " weeks OR " & return & theMonth as string) & " months" & return & "AGO"
end if
end if
-- After a calculation is performed, it will return to the main switchboard until the "Exit" button is clicked
end repeat
-- xx Athiya Deviyani 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment