Solved X minutes to days, hours, minutes format

  • 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 comminuty!

    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!

Status
Not open for further replies.

mrxqware

Member
Apr 19, 2017
17
0
0
23
Hey guys,

I would like to convert an amount of 'x minutes' to an output of 'x day(s), x hour(s), x minute(s).
I have no idea how to do that in an easy way.

I feel like I have to calculate every time how many days there fit in the 'x minutes', and then substract it, to calculate how many hours there are left, then also substract it, to get the 3rd value in minutes as well.

Is there an easier way to do this?
 
well, if it's a timespan, Skript automatically converts it to the next value, let's say I have 1440 minutes (1 day) in a string, like this:
code_language.skript:
set {_test} to "1440 minutes"
then I parse it as a timespan:
code_language.skript:
set {_test} to "1440 minutes" parsed as time span
then when you broadcast it or something, it'll be 1 day because Skript converted it. Here is a visual example if you didn't understand:
upload_2017-6-1_12-39-36.png
 
  • Like
Reactions: mrxqware
I love the solution!
Is there also a way to change it to 1d, 3h, 20m

I already tried to stringreplace hours to h, but that didn't do the trick
 
Last edited by a moderator:
I have an amount: "5349"
reading it from the database and it stands for 5349 minutes.

I would like to convert that to: 3d, 17h, 9m
 
code_language.skript:
command /test <number>:
    trigger:
        set {_start_time} to arg-1
        set {_days} to ({_start_time} / 1440)
        set {_days} to "%{_days}%"
        set {_time::*} to {_days} split at "."
        set {_days} to {_time::1}
        set {_hours} to {_time::2} parsed as a number
        set {_hours} to ({_hours} * 24 * (10^-2))
        set {_hours} to "%{_hours}%"
        set {_time::*} to {_hours} split at "."
        set {_hours} to {_time::1}
        set {_mins} to {_time::2} parsed as a number
        set {_mins} to ({_mins} * 60 * (10^-2))
        set {_mins} to "%{_mins}%"
        set {_time::*} to {_mins} split at "."
        set {_mins} to {_time::1}
        broadcast "%{_days}%d, %{_hours}%h, %{_mins}%m"

This works unless someone knows a simpler way. The minutes are slightly off due to rounding.
 
  • Like
Reactions: mrxqware
code_language.skript:
command /test <number>:
    trigger:
        set {_start_time} to arg-1
        set {_days} to ({_start_time} / 1440)
        set {_days} to "%{_days}%"
        set {_time::*} to {_days} split at "."
        set {_days} to {_time::1}
        set {_hours} to {_time::2} parsed as a number
        set {_hours} to ({_hours} * 24 * (10^-2))
        set {_hours} to "%{_hours}%"
        set {_time::*} to {_hours} split at "."
        set {_hours} to {_time::1}
        set {_mins} to {_time::2} parsed as a number
        set {_mins} to ({_mins} * 60 * (10^-2))
        set {_mins} to "%{_mins}%"
        set {_time::*} to {_mins} split at "."
        set {_mins} to {_time::1}
        broadcast "%{_days}%d, %{_hours}%h, %{_mins}%m"

This works unless someone knows a simpler way. The minutes are slightly off due to rounding.

Thank you very much! I tried it, but the minutes gave a wrong output, wasn't a difference of 1, but your code inspired me:

I fixed it this way:
Code:
       set {_parsedplaytime} to "%{_ptstring}%" parsed as time span
       set {_start_time} to "%{_parsedplaytime}%" parsed as string
       replace all " days" in {_start_time} with "d,"
       replace all " hours" in {_start_time} with "h,"
       replace all " and " in {_start_time} with " "
       replace all " minutes" in {_start_time} with "m"
 
Status
Not open for further replies.