Is it possible to create a countdown in days, hours, mins and secs?

0 favourites
  • 12 posts
From the Asset Store
Fully commented source code/event sheet & sprites to create a space shooter game
  • I've created a countdown in mins and secs previously but is it possible to create a countdown in days, hours, mins and secs? I need it displayed in boxes as text.

    If it's not possible to do in Construct, then I will need to simply delete the boxes and will not include the countdown, so not the end of the world.

  • Yes, use the date plugin expression date.now to get Unix time. Subtract that from your target Unix time. The resulting number is the number of milliseconds until the target time. Divide by 1000 to get seconds, 60000 to get minutes, 3600000 for hours, ect.

  • An example:

    Building_Upgrading&": "&zeropad(floor(UTR/84600),2)&"d "&zeropad(floor(UTR/60/60%60),2)&"h "&zeropad(floor(UTR/60%60),2)&"m "&zeropad(floor(UTR%60),2)&"s"

    UTR is my time variable (Global)

    With zeropad 1d 05h 01m 01s

    Without 1d 5h 1m 1s

  • Appreciate your help guys. I'm new to using Construct, so I'm a bit slower with things on here. Can you tell me how I go about setting the future date to subtract from? At the moment I'm using Date.Get(2021, 11, 11, 18, 0, 0, 0) but this doesn't seem to be working. It should be 12th December 2021 7pm, am I setting this correctly?

  • Look for the Date & time example in the start page of C3. Just type Date & time in the search box and you should find it.

    The example shows a bunch of things you can do with the Date plugin. Calculating the time between the current date and a future date is one of them.

    There are quite a few things going on in the example, so just ask again if there is something you don't understand after taking a look.

  • Thanks Diego. I've found that now and pretty much copied and pasted most of it into my game but it doesn't seem to be working and I'm not sure where I'm going wrong.

    The only parts that are different in my game is the GetDate function and Every Tick event.

    I've set the GetDate function to: Date.Get(2021, 11, 11, 18, 0, 0, 0). This should be December 12th 2021 at 7pm and I want the countdown to show the difference between this date and time, and the current date and time. Is the equation correct?

    The Every Tick event reads I have written like this:

    Functions.GetDateDifference(Date.Now, Functions.GetDate, "Days") &

    Functions.GetDateDifference(Date.Now, Functions.GetDate, "Hours") &

    Functions.GetDateDifference(Date.Now, Functions.GetDate, "Minutes") &

    Functions.GetDateDifference(Date.Now, Functions.GetDate, "Seconds")

    Is this correct? For some reason the text is being set to '1'.

    The other events (GetDateDifference and Trunc functions) are exactly the same in my game as on the example, do I need to change these or can I keep as they are?

    Sorry for all the questions - I'm quite new to Construct and still learning things.

  • It's hard to tell without looking at your project.

    If you take the example and just change the GetDate function to return Date.Get(2021, 11, 12, 19, 0, 0, 0), the count down works as expected. So I imagine that in the process of copying and pasting you maybe forgot something or there is something that needs to be changed and you missed it.

    The parameters for the Date.Get expression are as follows:

    year => 4 digit number
    month => 0 to 11
    day => 1 to 31
    hour => 0 to 23
    minutes => 0 to 59
    seconds => 0 to 59
    milliseconds => 0 to 999
    

    So Date.Get(2021, 11, 12, 19, 0, 0, 0) should get you the date for December 12th 2021 at 7pm

  • I'm going to assume this is only using local time on the users device (otherwise you'll need a server for your unix timestamp, which stops the user from cheating by adjusting their clock).

    Date.now returns the a unix timestamp (in Milliseconds) based on the users device time

    Floor(date.now/1000) returns the unix timestamp in seconds.

    Unix time is a way of representing a timestamp by representing the time as the number of seconds since January 1st, 1970 at 00:00:00 UTC. One of the primary benefits of using Unix time is that it can be represented as an integer making it easier to parse and use across different systems.

    For a simple count down:

    When your timer starts set a variable to current timestamp Floor(date.now/1000) plus the number of seconds you want to countdown. (for 1 minute it would be unix timestamp plus 60)

    Then every second (or add 1, 1dt, etc.) compare the current time vs the timestamp and display it. (using the above formatting for d/h/m/s)

  • Thanks a lot for the explanations, really appreciate it.

    I still can't seem to get my head around this so I've made a demo and copied a Dropbox link below. It's just a textbox but it should be displaying the countdown between now and December 12th but for whatever reason it's not working.

    Would you be able to take a quick look and see where I'm going wrong?

    dropbox.com/s/jttzie527hy93se/Countdown%20Demo.c3p

    Thanks very much for your help.

    Let me know if you can't access the link.

  • Try something like this:

  • I would probably do it like jsutton suggested...

    the reason your file was just showing 1 was because the action where you updated the text object saw a bunch of numbers and was doing a logical & with them and coming up with true (1).

    the example you started with can only show up to 31 for the days. I modified it by taking the modulo off the month (the % 30.417)

    https://www.rieperts.com/games/forum/DateCountdown.c3p

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • It works! Thanks so much guys, I'm so grateful! Jsutton, I used your method and it works perfectly.

    Not sure why they used a separate method on the example in the start page which is more complex - seems unnecessary to me.

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