Jump to content

Trying to find number of days between two dates

Recommended Posts

Hey Gang


Working on this workflow. I want to be able to put one date in, then another date, and have the result be the number of days between the two (Just days, not months, weeks, and days).


This (possibly klunky) AppleScript seems to generate the expected result, but I don't know how to get the result spat back out to Alfred so I can post it or whatever. I'm sure it's something simple, any help appreciated!

set firstDate to "20171231"
set resultDate1 to the current date
set the year of resultDate1 to (text 1 thru 4 of firstDate)
set the month of resultDate1 to (text 5 thru 6 of firstDate)
set the day of resultDate1 to (text 7 thru 8 of firstDate)
set the time of resultDate1 to 0

set secondDate to "20171201"
set resultDate2 to the current date
set the year of resultDate2 to (text 1 thru 4 of secondDate)
set the month of resultDate2 to (text 5 thru 6 of secondDate)
set the day of resultDate2 to (text 7 thru 8 of secondDate)
set the time of resultDate2 to 0

set answer to ((((resultDate1 - resultDate2) / 60) / 60) / 24)

return answer

NOTE: I'm using dates this way for my own preference, I get tons of these dates and need to quickly get the difference in number of days.

Link to comment
5 hours ago, roccitman said:

how to get the result spat back out to Alfred so I can post it or whatever.



That’s very unclear. You need to be more specific about what you want to do with the result. As it is, if you connect that code in a node to another node in Alfred, it’ll just pass the result.

Link to comment

Here is the fixed workflow.

Changes, in no particular order:

  • 1date and 2date changed to date1 and date2. Don’t start variable names with numbers. Seldom (if ever) a good idea (for more context, Stack Overflow answer, Quora answer).
  • Changed NSAppleScript node to Run Script node.
  • Last Args and Vars node was deleted. Its contents made no sense and it was unnecessary, anyway.
  • Large Type needs only {query} (i.e. output of last command).
  • The way you were calling variables was incorrect. Open the debugger and see the errors. For the correct way, see this post.
  • Changed set for return.
  • Changed as real to as integer. Seems more like what you would want as a result.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...