Help with formatting difference between two dates

  • Welcome to skUnity!

    Welcome to skUnity! This is a forum where members of the Skript community can communicate and interact. Skript Resource Creators can post their Resources for all to see and use.

    If you haven't done so already, feel free to join our official Discord server to expand your level of interaction with the community!

    Now, what are you waiting for? Join the community now!

  • LOOKING FOR A VERSION OF SKRIPT?

    You can always check out skUnity Downloads for downloads and any other information about Skript!

Poldup

Member
Aug 29, 2024
2
0
1
I'm trying to make a small chrono script displaying in action bar but I can't format the text as I would like

It works by calculating the difference between "now" and a previous value of "now" stored in a variable, but the result of this difference seems to be stored in a text variable, therefore I can't format it as "MM:SS"

Here is the script :
JavaScript:
function CountDisplay(p: player):       
    set {_minutes} to "00"   
    while {boatRacing::%{_p}'s uuid%} is true: #while the player is racing
        set {_time} to difference between {boatTimer::%{_p}'s uuid%} and now #{boatTimer::%{_p}'s uuid%} stores a previous value of now
        set {_timeDate} to {_time} parsed as date #I try to force parsing time as a date type but I receive an error telling me I can't parse text as date
        set {_seconds} to {_timedate} formatted as "ss" #This variable is never attributed, that's why I think I need a date or time type for "formatted as" to work
        send {_seconds} to {_p}
        set {_minutes} to {_timedate} formatted as "mm" #same here
        send action bar "%{_minutes}%:%{_seconds}%" to {_p} #the action bar says "<none>:<none>"
        wait 1 seconds
 
Yeah, you’re right.
Since the variable isn’t a text string, it doesn’t get converted. You can easily fix it by using:

CSS:
set {_placeholder} to "%{wanted variable for the string}%"

I’m not exactly sure why it behaves this way, but I made a little showcase script to help demonstrate it.

I hope this helps!


JavaScript:
command /timer:
    trigger:
        set {timer} to 7200

every second:
    loop all players:
        set {_t} to "%{timer}% ticks" parsed as timespan
        remove 1 from {timer}
        set {_time} to "%{_t}%"
        replace all " seconds" or " second" with "s" in {_time}
        replace all " minutes" or " minute" with "m" in {_time}
        replace all " and" with " :" in {_time}
        wait 5 tick
        send action bar "%{_time}%" to loop-player
 
Yeah, you’re right.
Since the variable isn’t a text string, it doesn’t get converted. You can easily fix it by using:

CSS:
set {_placeholder} to "%{wanted variable for the string}%"

I’m not exactly sure why it behaves this way, but I made a little showcase script to help demonstrate it.

I hope this helps!


JavaScript:
command /timer:
    trigger:
        set {timer} to 7200

every second:
    loop all players:
        set {_t} to "%{timer}% ticks" parsed as timespan
        remove 1 from {timer}
        set {_time} to "%{_t}%"
        replace all " seconds" or " second" with "s" in {_time}
        replace all " minutes" or " minute" with "m" in {_time}
        replace all " and" with " :" in {_time}
        wait 5 tick
        send action bar "%{_time}%" to loop-player
Thanks for your answer !

The thing is I don't want the variable to be a text, I don't get how the difference between two dates (now and earlier now) can be a text and not a timespan.

What I don't like in your solution is that it does smthing every second whereas in mine it is a function that can be called or not (actually I sometimes activate the display of the chronometer, sometimes not if there is too many players on the server so it doesn't lag). And when I don't display the chronometer, I don't have to actually count every second, only store the starting date (and time) of the timer and the ending one and then displaying the difference between the two.

I would like to do the same when displaying the chronometer, only displaying this difference every second, formatted as "MM:SS" which I can't since I can't get the difference other than a text looking like "1 minutes and 15.6 seconds"...
 
Thanks for your answer !

The thing is I don't want the variable to be a text, I don't get how the difference between two dates (now and earlier now) can be a text and not a timespan.

What I don't like in your solution is that it does smthing every second whereas in mine it is a function that can be called or not (actually I sometimes activate the display of the chronometer, sometimes not if there is too many players on the server so it doesn't lag). And when I don't display the chronometer, I don't have to actually count every second, only store the starting date (and time) of the timer and the ending one and then displaying the difference between the two.

I would like to do the same when displaying the chronometer, only displaying this difference every second, formatted as "MM:SS" which I can't since I can't get the difference other than a text looking like "1 minutes and 15.6 seconds"...

Hey, you don't need to use "every second"—that was just an example. You can't edit numbers directly; you have to turn the number into a text variable with quotation marks like this: "". It's because of how script variables are stored and processed.

CSS:
        set {_t} to "%{timer}% ticks" parsed as timespan
        set {_time} to "%{_t}%"
        replace all " seconds" or " second" with "s" in {_time}
        replace all " minutes" or " minute" with "m" in {_time}
        replace all " and" with " :" in {_time}

That would be the easiest way. I think dates are calculated differently and might need other parameters. The best way, I think, to show a clean timer is simply to edit the individual text elements.