How do I show the UTC timestamp with leading zeros using the date/time object? Example : HH:MM:SS

1 favourites
  • 8 posts
From the Asset Store
An educational game for Times Table. An easy to use template for developers to build larger games
  • I'm making a timer for my Girlfriend because she has an online job with repeated timed tasks. There are plenty of countdown timers, but I am interested in Construct 3, so I figured it would be a good beginner's task to create the timer.

    I've successfully created a simple timer that counts down in seconds; however, I would like to start learning how to use the date object correctly in this updated timer project. Unfortunately, I'm an advanced graphic artist and novice visual coder, so it's hard to wrap my brain around a few concepts.

    The "Novice timer project" has presented me with a few problems that I'd appreciate help solving if possible.

    1. How do I display a UTC timestamp with leading zeros In a 12-hour format?

    -(00:00:00) the example shows numbers like this: "3:3:4"

    2. How do I then display: "UTC:" 00:00:00 with the addition of an AM or PM meridiem?(AM/PM)

    so, in conclusion, the outcome would hopefully look like this:

    Tuesday, June 7, 2022 UTC: 5:13 PM - MT: 11:13 AM

    (*or something similar*)

    both (UTC) and Mountain Time (MT) in 12-hour format (MT) Please and thank you.

    Any direction you might be able to point me in would be greatly appreciated.

    I have included both a screenshot and the project file sort of explaining things.

    Construct File : https://www.dropbox.com/s/6dlvjptu7i5kylf/Timer_Example.c3p?dl=0

    Tagged:

  • Here is the text that I used in the event (for the Date_Time_Text object)

    Date.ToLocaleDateString(Date.Now) &" "&"(UTC : "&Date.GetUTCHours(Date.Now)&":"&Date.GetUTCMinutes(Date.Now)&":"&Date.GetUTCSeconds(Date.Now)&")"&" (GMT : "&Date.ToLocaleTimeString(Date.Now)&")"

  • You're going to want to use the zeropad system expression to add leading zeroes.

    zeropad(number, digits)

    Pad number out to a certain number of digits by adding zeroes in front of the number, then returning the result as a string. For example, zeropad(45, 5) returns the string "00045"

    For changing a 24 hour format to a 12 hour format, use modulo %, which is the remainder after dividing. Note that "0" should actually read 12 in this case, so you could add a conditional expression

    hour%12=0?12:hour%12
    

    This reads "If the remainder of hours after dividing by 12 is 0, then 12, else the remainder of hours after dividing by 12."

    Adding zeropad to that would look like this -

    hour%12=0?12:zeropad(hour%12,2)
    

    And it is always a good idea to int() or round() to account for rounding errors -

    int(hour%12)=0?12:zeropad(int(hour%12),2)
    

    To determine AM or PM for the hours, divide hours by 12. 0-11 are AM, and 12-23 are PM. You can use another conditional expression -

    int(hour/12)<1?"AM":"PM"
    
  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Alright, Thank you.

    I'm completely lost, perhaps I got in over my head on this one!

    I barely understand Expressions, I feel like I'm supposed to add that somewhere in the text but it's really eluding me. (I'm rather a novice)

    I appreciate the quick response, I'd probably better stick to my guns.

  • You're already using expressions. Date.Now is an expression that gives you the current time in unix timestamp format. Date.GetUTCHours gets you the hours from a timestamp, so you put Date.Now in Date.GetUTCHours, resulting in Date.GetUTCHours(Date.Now)

    Now if you want to zeropad the resulting number, you use the zeropad expression. It would be zeropad(Date.GetUTCHours(Date.Now),2), where Date.GetUTCHours(Date.Now) is the number you want padded and 2 is the amount of padding.

  • Thank you so much for the responses again, Colonel Justice: I think that plugin is gonna work perfectly for what I need.

  • You're already using expressions. Date.Now is an expression that gives you the current time in unix timestamp format. Date.GetUTCHours gets you the hours from a timestamp, so you put Date.Now in Date.GetUTCHours, resulting in Date.GetUTCHours(Date.Now)

    Now if you want to zeropad the resulting number, you use the zeropad expression. It would be zeropad(Date.GetUTCHours(Date.Now),2), where Date.GetUTCHours(Date.Now) is the number you want padded and 2 is the amount of padding.

    I really appreciate your detailed responses. thank you so much for your patience with me.

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