Count down date

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

croivo
Posts: 111
Joined: Wed Feb 26, 2014 11:02 pm

Count down date

Post by croivo » Tue Jul 01, 2014 1:41 am

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.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10333
Joined: Wed May 06, 2009 2:28 pm

Re: Count down date

Post by dunbarx » Tue Jul 01, 2014 3:01 am

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

croivo
Posts: 111
Joined: Wed Feb 26, 2014 11:02 pm

Re: Count down date

Post by croivo » Tue Jul 01, 2014 3:51 pm

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...

sefrojones
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 447
Joined: Mon Jan 23, 2012 12:46 pm

Re: Count down date

Post by sefrojones » Tue Jul 01, 2014 4:32 pm

Have you checked out the scripting conference stack for Date and Time?

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


--Sefro

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Count down date

Post by jmburnod » Tue Jul 01, 2014 5:46 pm

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
https://alternatic.ch

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10333
Joined: Wed May 06, 2009 2:28 pm

Re: Count down date

Post by dunbarx » Wed Jul 02, 2014 12:20 am

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

croivo
Posts: 111
Joined: Wed Feb 26, 2014 11:02 pm

Re: Count down date

Post by croivo » Wed Jul 02, 2014 1:34 am

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"

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: Count down date

Post by Simon » Wed Jul 02, 2014 1:40 am

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
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

SparkOut
Posts: 2947
Joined: Sun Sep 23, 2007 4:58 pm

Re: Count down date

Post by SparkOut » Wed Jul 02, 2014 7:54 am

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.

croivo
Posts: 111
Joined: Wed Feb 26, 2014 11:02 pm

Re: Count down date

Post by croivo » Wed Jul 02, 2014 1:41 pm

Can somebody give me example, how to 'add 20 days' for example to time I've converted do dateItems? Or subtract...

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Count down date

Post by Klaus » Wed Jul 02, 2014 1:45 pm

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

croivo
Posts: 111
Joined: Wed Feb 26, 2014 11:02 pm

Re: Count down date

Post by croivo » Wed Jul 02, 2014 2:17 pm

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...

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Count down date

Post by Klaus » Wed Jul 02, 2014 2:22 pm

Sorry, no idea what your script should do?

croivo
Posts: 111
Joined: Wed Feb 26, 2014 11:02 pm

Re: Count down date

Post by croivo » Wed Jul 02, 2014 2:27 pm

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.

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Count down date

Post by Klaus » Wed Jul 02, 2014 3:32 pm

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?

Post Reply