Need Help With Comparing Time

Not favoritedFavorited Favorited 0 favourites
  • 5 posts
From the Asset Store
An educational game for Times Table. An easy to use template for developers to build larger games
  • Ok well I'm trying out Date plugin and I'm trying to make this app that tells you when its sunrise and how long it will be in.(SS below)

    Well all I can't do is find out how to check the difference between the current time and sunrise then output the answer in minutes.

    Thanks,

    Radkampfwagen

  • Why do you use Browser.execJS? All these operations can be done with the Date plugin.

    Also, your 4th event on the screenshot will run AJAX request on every tick while the condition is true - the server may not like it and will ban your IP address.

    Can you show the AJAX response - JSON contents?

    To convert the current time to minutes use these expressions:

    // Minutes since midnight (UTC time)
    int((Date.now % 86400000) / 60000)
    
    // Minutes since midnight (local time)
    Date.GetHours(Date.Now)*60+Date.GetMinutes(Date.Now)
    
    // Minutes since the start of the current hour (local time)
    Date.GetMinutes(Date.Now)
    
    
  • I used Browser.execJS because I wanted to get the time for London and I wasnt sure about how to do it with Date plug=in.

    This is all the AJAX I use:

    + System: On start of layout

    -> AJAX: Request "https://api.aladhan.com/v1/timingsByCity?city=Blackburn&country=United%20Kingdom&method=2" (tag "")

    + AJAX: On "" completed

    -> JSON: Parse JSON string AJAX.LastData

    -> TextSunrise: Set text to "Sunrise:" &newline&JSON.Get("data.timings.Sunrise")

    -> TextSunset: Set text to "Sunset:"&newline&JSON.Get("data.timings.Sunset")

    + TextTime: Text is "00:00" (Ignore case)

    -> Timer: Start Timer "TimerMidnight" for 1 (Once)

    -> AJAX: Request "https://api.aladhan.com/v1/timingsByCity?city=Blackburn&country=United%20Kingdom&method=2" (tag "")

    heres the link:

    api.aladhan.com/v1/timingsByCity

    Also will this trigger once?

    + TextTime: Text is "00:00:00" (Ignore case)

    rather that "00:00"?

  • Also will this trigger once?

    + TextTime: Text is "00:00:00" (Ignore case)

    No, if someone runs your app on a 240 Hz monitor, this event will trigger 240 times per second and send 240 requests to the server.

    Also, comparing the time to "00:00:00" to detect the start of a new day is a bad idea, because the app might not be active at that exact second. Instead, save the current day in a variable and compare it every few seconds. If the day has changed - update the variable and send a new request to the server.

    As for comparing the time to sunrise - the server returns it in "HH:MM" format, you can use tokenat to extract the hours and minutes from the string.

    Local string sunriseTime‎ = 07:25
    Local string currentTime‎ = 04:07
    Local number minutesToSunrise‎ = 0
    
    -----> System: Set minutesToSunrise to (int(tokenat(sunriseTime, 0, ":"))-int(tokenat(currentTime, 0, ":")))×60 + (int(tokenat(sunriseTime, 1, ":"))-int(tokenat(currentTime, 1, ":"))) 
    
    --------+ If minutesToSunrise ≥ 0
    ---------> Text: Set text to int(minutesToSunrise÷60) & " hours " & zeropad(minutesToSunrise%60, 2) & " minutes to sunrise"
    
    --------+ Else
    ---------> Text: Set text to "Past the sunrise time"
    

    You can copy/paste this into your event sheet:

    {"is-c3-clipboard-data":true,"type":"events","items":[{"eventType":"block","conditions":[],"actions":[],"children":[{"eventType":"variable","name":"sunriseTime","type":"string","initialValue":"07:25","comment":"","isStatic":false,"isConstant":false},{"eventType":"variable","name":"currentTime","type":"string","initialValue":"04:07","comment":"","isStatic":false,"isConstant":false},{"eventType":"variable","name":"minutesToSunrise","type":"number","initialValue":"0","comment":"","isStatic":false,"isConstant":false},{"eventType":"block","conditions":[],"actions":[{"id":"set-eventvar-value","objectClass":"System","parameters":{"variable":"minutesToSunrise","value":"(int(tokenat(sunriseTime, 0, \":\"))-int(tokenat(currentTime, 0, \":\")))*60 +\r\n(int(tokenat(sunriseTime, 1, \":\"))-int(tokenat(currentTime, 1, \":\")))\r\n"}}],"children":[{"eventType":"block","conditions":[{"id":"compare-eventvar","objectClass":"System","parameters":{"variable":"minutesToSunrise","comparison":5,"value":"0"}}],"actions":[{"id":"set-text","objectClass":"Text","parameters":{"text":"int(minutesToSunrise/60) & \" hours \" & zeropad(minutesToSunrise%60, 2) & \" minutes to sunrise\""}}]},{"eventType":"block","conditions":[{"id":"else","objectClass":"System"}],"actions":[{"id":"set-text","objectClass":"Text","parameters":{"text":"\"Past the sunrise time\""}}]}]}]}]}
    
  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • You're The Best!

    everything working now.

    Thanks a lot dop2000

Jump to:
Active Users
There are 0 visitors browsing this topic (0 users and 0 guests)