Page 1 of 1

Checking and comparing Date and Time

Posted: Sat Sep 02, 2017 11:51 am
by Flo92
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!!

Re: Checking and comparing Date and Time

Posted: Sat Sep 02, 2017 1:26 pm
by jmburnod
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

Re: Checking and comparing Date and Time

Posted: Sun Sep 03, 2017 5:07 pm
by dunbarx
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

Re: Checking and comparing Date and Time

Posted: Sun Sep 03, 2017 9:06 pm
by Flo92
Thanks a lot to both of you! I managed to do it!

Re: Checking and comparing Date and Time

Posted: Mon Sep 04, 2017 2:58 am
by [-hh]
Don't forget to think about what to do when a user changes his system time and/or disconnects from internet ;-)

Re: Checking and comparing Date and Time

Posted: Mon Sep 04, 2017 12:53 pm
by MaxV
Do you know that there is a specific date/time library? See https://github.com/derbrill/libdate