Jump to content
roccitman

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.

Share this post


Link to post
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.

Share this post


Link to post

Another note, you'll see in the workflow, I am trying to pass variables into the script and I think that's partly to blame. In the script sample above it's just hard coded dates.

Share this post


Link to post

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.

Share this post


Link to post
16 minutes ago, roccitman said:

Oh wow, ok learning a lot. Thank you thank you!

Glad to help.

 

16 minutes ago, roccitman said:

And have a great Turkey Day

Thank you, but I’m Portuguese, these dates have no special meaning to us. A happy thanksgiving to you!

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...