Page 1 of 2

Count down date

Posted: Tue Jul 01, 2014 1:41 am
by croivo
I have fields 'year', 'month', 'date' and 'hour'. What I want is to make count-down script so it will count down the time left to that 'event'. I have no idea how to do this.

Re: Count down date

Posted: Tue Jul 01, 2014 3:01 am
by dunbarx
Hi,

Look up such things as "dateitems", "seconds" and "convert" in the dictionary. Now assemble your disparate data into a form that LC can work with.

Write back if you get stuck. But do try to figure this out before doing so. Date and time stuff is really a lot of fun for a new user.

Craig Newman

Re: Count down date

Posted: Tue Jul 01, 2014 3:51 pm
by croivo
Yeah I looked for "dateItems", "seconds" and "convert", I understand that but when I need to 'subtract' that 'final' time from current time I have problems... Need to set somehow minutes and seconds to max 60, hours to max 24 etc...

Re: Count down date

Posted: Tue Jul 01, 2014 4:32 pm
by sefrojones
Have you checked out the scripting conference stack for Date and Time?

http://www.hyperactivesw.com/revscriptc ... ences.html


--Sefro

Re: Count down date

Posted: Tue Jul 01, 2014 5:46 pm
by jmburnod
Hi croivo,
I use seconds to add or subtract time.
On day have 86400 seconds and that is enough for my needs
Best regards
Jean-Marc

Re: Count down date

Posted: Wed Jul 02, 2014 12:20 am
by dunbarx
Hi.
On day have 86400 seconds and that is enough for my needs
What Jean-Marc means is that if you do the math in seconds, all the minute/day stuff goes away. The dateItems is very useful here, going both ways, into and out of seconds.

Now this will only give you integral days, actually taken from the same time in each day. But it obviates leap year issues, and is a great technique to learn. Practice with a few test scripts. Try to work this so that you can, for example, get a counter from noon on Tuesday to dinner on Friday. That sort of thing. Write back if you run into problems.

Craig

Re: Count down date

Posted: Wed Jul 02, 2014 1:34 am
by croivo
Thanks to all for offering your help. Here is what I tried to do: convert both current and "expiry" time to seconds, than subtract these two and convert result to the time. And that kinda works, but it's absolutely not precisely and I know there is much easier and efficient way to do this...

Code: Select all

--CURRENT TIME CONVERT TO SECONDS
   put long system time into field "CURRENT"
   put field "CURRENT" into ctime
   set itemDel to colon
   put item 1 of ctime into chours
   put item 2 of ctime into cminutes
   put item 3 of ctime into cseconds
   multiply chours by 3600
   multiply cminutes by 60
   add chours to cminutes
   add cminutes to cseconds
   
   --FINAL TIME CONVERT TO SECONDS
   put field "FINAL" into ftime
   set itemDel to colon
   put item 1 of ftime into fhours
   put item 2 of ftime into fminutes
   put item 3 of ftime into fseconds
   multiply fhours by 3600
   multiply fminutes by 60
   add fhours to fminutes
   add fminutes to fseconds
   
   --SUBTRACT
   subtract cseconds from fseconds
   convert fseconds to long system time
   put fseconds into field "REMAINING"

Re: Count down date

Posted: Wed Jul 02, 2014 1:40 am
by Simon
Hi croivo,
For working with date time stuff you Really want to use dateItems.
It will do all the leap year stuff and the days...

Simon

Re: Count down date

Posted: Wed Jul 02, 2014 7:54 am
by SparkOut
What Simon said. You can calculate offsets outside the normal range automatically - I mean, say the day of the month is 20 and you want to add 20 days, you just add 20 to the day of the month and it works out what day of the next month that is, and adjusts all the rest of the data. Easy.

Re: Count down date

Posted: Wed Jul 02, 2014 1:41 pm
by croivo
Can somebody give me example, how to 'add 20 days' for example to time I've converted do dateItems? Or subtract...

Re: Count down date

Posted: Wed Jul 02, 2014 1:45 pm
by Klaus
Hi croivo,

a look into the dictionary at "dateitems" might be very enlightening! 8)
...
add 20 to item 3 of tVarWithDateitemsInIt
convert tVarWithDateitemsInIt from dateitems to long system date
answer ("This is 20 days later:" && tVarWithDateitemsInIt)
...


Best

Klaus

Re: Count down date

Posted: Wed Jul 02, 2014 2:17 pm
by croivo
Ok so I need something like this

Code: Select all

on mouseUp
   put "15:00" into field "final"
   convert field "final" to dateItems
   put long system time into field "current"
   convert field "current" to dateItems
   
   subtract item 1 of field "current" from item 1 of field "final" 
   subtract item 2 of field "current" from item 2 of field "final" 
   subtract item 3 of field "current" from item 3 of field "final" 
   subtract item 4 of field "current" from item 4 of field "final" 
   subtract item 5 of field "current" from item 5 of field "final" 
   subtract item 6 of field "current" from item 6 of field "final" 
end mouseUp
but this is not working right...

Re: Count down date

Posted: Wed Jul 02, 2014 2:22 pm
by Klaus
Sorry, no idea what your script should do?

Re: Count down date

Posted: Wed Jul 02, 2014 2:27 pm
by croivo
I need to 'count down' the time until some 'event'. So for example now is 15:00, and my 'event' starts in 18:00. I want to count-down time left to that event.

Re: Count down date

Posted: Wed Jul 02, 2014 3:32 pm
by Klaus
So you want to display something like this until 0:00 has been reached:
3:00 ## Start countdown
2:59
2:58
...
0:01
0:00 ## Stop countdown

Right?