Page 1 of 2

How many days until ...

Posted: Thu Jul 12, 2018 1:49 pm
by rjd
Hi Livecoders - how can I calculate how much time it will be until next Saturday?

Re: How many days until ...

Posted: Thu Jul 12, 2018 1:59 pm
by Klaus
Hi rjd,

there are many ways to do so in LC!
One way:

Code: Select all

...
put the date into tStart
put "7/14/18" into tEnd
convert tStart to seconds
convert tEnd to seconds
put tEnd - tStart into tDuration
answer "Duration until saturday:" && smpte_lite(tDuration)
...

## Will convert seconds to time code HH:MM:SS
function smpte_lite dauer1
  set the numberformat to xx
  put dauer1 div 60 into minu  
  put minu div 60 into stunden 
  put minu mod 60 into minu  
  put dauer1 mod 60 into seku
  return stunden & ":" & minu & ":" & seku
end smpte_lite
Best

Klaus

Re: How many days until ...

Posted: Thu Jul 12, 2018 2:02 pm
by rjd
Ah that looks simple enough -- but what if I don't know the date of the 'next' Saturday (or whatever day a user chooses)?

Re: How many days until ...

Posted: Thu Jul 12, 2018 2:16 pm
by Klaus
What do you know about the date in the future? Mind-reading won't work (yet)! :-)
I mean how will the user tell you what he wants?

Re: How many days until ...

Posted: Thu Jul 12, 2018 2:21 pm
by rjd
Mind reading would be good to have sometimes when dealing with co-workers =( I'm working on sending reminders on a certain day. If the reminder day is a Saturday (regardless of the date of that Saturday), I was hoping to have a basic countdown timer of sorts to display to the user. Something like ... 4d 3h 24m until Saturday.

Re: How many days until ...

Posted: Thu Jul 12, 2018 2:29 pm
by Klaus
OK, get it, but in the moment I have no quick and simple idea on how to find out how to calculate the "current difference to saturday". Know what I mean?

Re: How many days until ...

Posted: Thu Jul 12, 2018 3:01 pm
by rjd
Yes, I know what you mean -- as with the majority of tasks done with Livecode, and as you noted, there are many ways to do something. I'm sure another cool solution will surface here 8)

Re: How many days until ...

Posted: Thu Jul 12, 2018 3:09 pm
by Klaus
This also depends on how your interface will look, resp. how your user will let you know what he wants.

If you provide a mini calendar, then this is a no-brainer. If you let the user enter a day of week, that will be a TAD more complicated and will need a lot of scripting.

Here some food for thought:
In LC we have -> the weekdaynames
This is where you could check the current day of week against the user-entered day

We have -> dateitems
Where we could get the current day of week, read up the entry in the dictionary.

A combination from my last script and these things mentioned above will be able to do what you want to. Can you cut your task into little pieces and solve these according to the things mentioned above?

Re: How many days until ...

Posted: Thu Jul 12, 2018 3:47 pm
by dunbarx
Klaus wrote:
Can you cut your task into little pieces and solve these according to the things mentioned above?
How come he gets a pass with this sort of reply, and I don't???

Craig

Re: How many days until ...

Posted: Thu Jul 12, 2018 3:52 pm
by Klaus
dunbarx wrote:
Thu Jul 12, 2018 3:47 pm
How come he gets a pass with this sort of reply, and I don't???
Sorry, don't quite understand this surely funny injection?

Re: How many days until ...

Posted: Thu Jul 12, 2018 4:00 pm
by dunbarx
Klaus.

From "Our Klaus"
I hate to break it to you all Klaus is actually a German AI BOT.

It's a Livecode version of Joseph Weizenbaum's Eliza with artificial "Sarcasm" built in and an initial "German Humour" module added.

"Craig" on the other hand is a fork of "Klaus" using recursion and exits the call early when the stack overflows and thus has random questions at the end like "Post Back", "Ask for help?", "More?" this is to hide the fact that this experimental BOT has run out of memory.
XXX

Craig

Re: How many days until ...

Posted: Thu Jul 12, 2018 4:02 pm
by Klaus
Ah, OK, get it, maybe we should really put some more emphasis on the I in AI! :D

Re: How many days until ...

Posted: Thu Jul 12, 2018 4:03 pm
by rjd
Thank you for your suggestions and some good hints on how to approach this. I'll give it a go :D

Re: How many days until ...

Posted: Thu Jul 12, 2018 4:08 pm
by Klaus
rjd wrote:
Thu Jul 12, 2018 4:03 pm
Thank you for your suggestions and some good hints on how to approach this. I'll give it a go :D
Great, please get back here when you get stuck!

Re: How many days until ...

Posted: Thu Jul 12, 2018 4:58 pm
by jacque
The last number in the dateItems is the day of the week. If you are aiming for the next Saturday then convert today's date to dateItems and subtract that number from 7, and add the difference to the last item of today's dateItems. Now you have the date of next Saturday. Convert that back to whatever date format you want.