# Solved Timespan to format

Discussion in 'Skript' started by Jordan, Apr 30, 2018.

Not open for further replies.
1. ### Jordan Member

Joined:
Jan 25, 2017
Messages:
190
12
Hello,

Mans was never the best at the old mathEMATICS at school so he is stuck on problem.

How does one convert X seconds into a nicely formatted Days, Hours, Minutes and seconds

I get you can convert a timespan into ticks, days, hours. minutes, seconds ect. with SKstuff but only works when you convert a timespan into a single hour or minute.. This does not work

Set {_test} to "86400 Seconds" parsed as timespan to converted days, hours, minutes and seconds

Just returns 1 day, 1 hour, 1 minute, 1 second

I read up on integer division and some operator like % but then got confused and gave up

Much help would be appreciated.

I'm also aware you can display a timespan but it does not display Days, Hours, Minutes and seconds. all at once.

#1
Post #9 by Jordan, May 2, 2018
3. ### JustADev Well-Known Member

Joined:
Apr 8, 2017
Messages:
380
9
Use @EWS's snippit for that,
Code (Skript):
1. function formatseconds(n: number) :: timespan:
2.     set {_a} to "%{_n}% seconds" parsed as timespan
3.     set {_a} to "%{_a}%"
4.     replace all " seconds" or " second" with "sec" in {_a}
5.     replace all " minutes" or " minute" with "min" in {_a}
6.     replace all " hours" or " hour" with "h" in {_a}
7.     replace all " days" or " day" with "d" in {_a}
8.     return {_a}

#2
• Friendly x 1
4. ### Jordan Member

Joined:
Jan 25, 2017
Messages:
190
12
As i said in my post i already know about this, yet it's inconsistent very rarely displays the seconds and does not actually calculate times down to the decimals. Rounds down instead of up ect. This might be my skript version though if this is working for you i guess.

#3
5. ### JustADev Well-Known Member

Joined:
Apr 8, 2017
Messages:
380
9
usually, the "difference between {_Var} and now" will give you a precise answer, in decimals if that is what you are looking for

#4
6. ### Jordan Member

Joined:
Jan 25, 2017
Messages:
190
12
Yeah but timespan will only display Hours and minutes. It'll display excess days and hours as decimals but is not accurate

#5
7. ### JustADev Well-Known Member

Joined:
Apr 8, 2017
Messages:
380
9
What do you mean "not accurate", like meaning that the timespans are rounded to a certain place? or what?

#6
8. ### Jordan Member

Joined:
Jan 25, 2017
Messages:
190
12
When i ran test it was a couple seconds behind and again timespans only show hours and minutes which is not ideal.

#7
9. ### Snow-Pyon ModeratorResource Staff

Joined:
Jan 25, 2017
Messages:
1,236
176
Medals:
There is really simple solution for this:
Code (Skript):
1. set {_test} to "1200 ticks" parsed as timespan

#8
10. ### Jordan Member

Joined:
Jan 25, 2017
Messages:
190
12
What version of skript do you use? because timespan is only displayed as hours and minutes in mine?
--- Double Post Merged, May 2, 2018, Original Post Date: May 2, 2018 ---
Turns out skstuff's convert methods are broken, or some shit to do with timespans being broken as this should work i think?

command /testtime:
trigger:
set {test} to "86460 seconds" parsed as timespan
set {_GetDays} to {test} converted to days
Set {_GetMinutes} to "%{_GetDays}%" parsed as timespan converted to minutes
remove "%{_GetDays}% days" parsed as timespan from {_GetMinutes}
broadcast "Days %{_GetDays}%, Minutes %{_GetMinutes}%"

However just returns an internal error in chat and a bunch of errors in console to do with skstuff.

#9
Not open for further replies.