Checking and comparing Date and Time

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

Post Reply
Flo92
Posts: 16
Joined: Fri Jul 21, 2017 7:15 pm

Checking and comparing Date and Time

Post by Flo92 » Sat Sep 02, 2017 11:51 am

Hi everyone!
Me again with another - hopefully for this app atleast - last question.
In my app I only allow the user to do things in certain time spans. So for example: The user is only allowed to use the app on the first day inbetween 3pm and 5pm on the second day inbetween 9am and 11am and so on. Then on the seventh day the user isn't allowed to use the app anymore (it's a medical study, that's why)

What I'd do is to just lock the screen if the current date and time isn't one of those allowed times and otherwise he can answer the survey in my app.

What I kinda thought would be the way is that I save the date + time into two hidden fields when the user opens the app for the first time - that's my StartDate and StartTime. Then whenever the user opens the app I'd do a "on preopenStack"-command to check for the current date and time and compare it with my StartDate/StartTime - I thought of some switch-case-scenario kinda like "case actualdate = startdate+1" if that is the case then some "if-command" for the time-span.
But atleast as I tried it I didn't get it to work yet, so I was hoping that someone could give me some advise for this, how to do it better.

Thanks in advance!!

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2718
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: Checking and comparing Date and Time

Post by jmburnod » Sat Sep 02, 2017 1:26 pm

Hi,
I save the date + time into two hidden fields
Why don't use the seconds to define the available times ?
If those flelds are in your standalone, don't forget that a standalone can't be save
Best regards
Jean-Marc
https://alternatic.ch

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9660
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Checking and comparing Date and Time

Post by dunbarx » Sun Sep 03, 2017 5:07 pm

After reading your post I instantly knew the "seconds" as the way forward. Then I saw that Jean-Marc posted the very same idea.

So, not being sure of how far along with LC you are, do you see how this might work? (pseudo)

Code: Select all

on openstack
  if this is the first opening, set a custom property (say, "the startTime") with the seconds. Also load your start/end pairs of clock times
  --for each subsequent opening, if the seconds - the startTime is between pairs of two pre-defined values
  switch
   case the seconds - starTime is between timeValuePair1
    do firstDayStuff
    break[
   case the seconds - starTime is between timeValuePair2
    do secondDayStuff
    break[
  end switch
You have to calculate the translation from the seconds to the start/end value pairs which are likely in time/date format. This is probably best be done on that very first opening of the app. You know, there are 86,400 seconds in a day, 3600 in an hour, that sort of thing. A little bit of thought may be required to isolate that first opening from all the others. You OK with that?

Craig Newman

Flo92
Posts: 16
Joined: Fri Jul 21, 2017 7:15 pm

Re: Checking and comparing Date and Time

Post by Flo92 » Sun Sep 03, 2017 9:06 pm

Thanks a lot to both of you! I managed to do it!

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Checking and comparing Date and Time

Post by [-hh] » Mon Sep 04, 2017 2:58 am

Don't forget to think about what to do when a user changes his system time and/or disconnects from internet ;-)
shiftLock happens

MaxV
Posts: 1579
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

Re: Checking and comparing Date and Time

Post by MaxV » Mon Sep 04, 2017 12:53 pm

Do you know that there is a specific date/time library? See https://github.com/derbrill/libdate
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”