FmtSecs() : Converts Seconds to Days,Hours,Minutes,Seconds

Post your working scripts, libraries and tools
User avatar
SKAN
Posts: 853
Joined: 29 Sep 2013, 16:58

FmtSecs() : Converts Seconds to Days,Hours,Minutes,Seconds

16 Jun 2020, 19:54

FmtSecs(nSeconds, FormatStr)
Splits nSeconds to individual values: 1: Days, 2: Hours, 3: Minutes, 4: Seconds and additionally 5: TotalHours, 6: TotalMinutes.
If you are not familiar with Format(), do ask for a specific example.

The function: (along with an example)

Code: Select all

FmtSecs(T, Fmt:="{:}d {:02}h {:02}m {:02}s") { ; v0.50 by SKAN on D36G/H @ tiny.cc/fmtsecs
Local D, H, M, HH, Q:=60, R:=3600, S:=86400
Return Format(Fmt, D:=T//S, H:=(T:=T-D*S)//R, M:=(T:=T-H*R)//Q, T-M*Q, HH:=D*24+H, HH*Q+M)
}

MsgBox % FmtSecs(1219512)                                  ; 14d 02h 45m 12s
. "`n" . FmtSecs(1219512, "{1:}d {2:}h {3:}m {4:}s")       ; 14d 2h 45m 12s
. "`n" . FmtSecs(1219512, "{1:02}:{2:02}:{3:02}:{4:02}")   ; 14:02:45:12
. "`n" . FmtSecs(1219512, "{5:04}h {3}m {4:}s")            ; 0338h 45m 12s
. "`n" . FmtSecs(1219512, "{6:} minutes & {4:02} seconds") ; 20325 minutes & 12 seconds
. "`n" . FmtSecs(1219512, "{6:06}:{4:02} mins")            ; 020325:12 mins
User avatar
rommmcek
Posts: 1123
Joined: 15 Aug 2014, 15:18

Re: FmtSecs() : Converts Seconds to Days,Hours,Minutes,Seconds

16 Jun 2020, 21:29

Thanks, is useful!
P.s.: Last line comment, there is a typo, should be: ; 020325.2 mins
User avatar
SKAN
Posts: 853
Joined: 29 Sep 2013, 16:58

Re: FmtSecs() : Converts Seconds to Days,Hours,Minutes,Seconds

17 Jun 2020, 08:05

Thanks for the feedback, @rommmcek :)
rommmcek wrote:
16 Jun 2020, 21:29
P.s.: Last line comment, there is a typo, should be: ; 020325.2 mins
The number after the dot is not a decimal fraction, but the number of seconds.
User avatar
rommmcek
Posts: 1123
Joined: 15 Aug 2014, 15:18

Re: FmtSecs() : Converts Seconds to Days,Hours,Minutes,Seconds

17 Jun 2020, 09:26

Sorry for the superficial remark (only looked at the right side of the semicolons and calculated by heart)! So to prevent misunderstanding I would put colon : in place of dot ..
Anyways very smart usage of the Format() function as a return value!
[Edit]: Last line suggestion: . "`n" . FmtSecs(1219512, "{6:06}:{4:02} [mins:secs]") ; 020325:12 [mins:secs]
User avatar
SKAN
Posts: 853
Joined: 29 Sep 2013, 16:58

Re: FmtSecs() : Converts Seconds to Days,Hours,Minutes,Seconds

17 Jun 2020, 09:54

rommmcek wrote:
17 Jun 2020, 09:26
to prevent misunderstanding I would put colon : in place of dot ..
I have edited the example. Thanks.
I picked the number 1219512 from MSDN GetDurationFormatEx() example.
I am actually planning to count down 2 hours, so 119.59m with a dot looks aesthetically good rather than with a colon.


:)
User avatar
SKAN
Posts: 853
Joined: 29 Sep 2013, 16:58

FmtSecs()

31 Aug 2020, 23:40

To format media duration we may use {1:}:{2:02}:{3:02}:{4:02} as FormatStr and simply remove leading zeroes and colons with LTrim()

Examples:
 

Code: Select all

#NoEnv
#SingleInstance, Force

Seconds := 61
FormattedSeconds := FmtSecs(Seconds, "{1:}:{2:02}:{3:02}:{4:02}")
MsgBox % FormattedSeconds
FormattedSeconds := LTrim(FormattedSeconds, "0:")
MsgBox % FormattedSeconds
 
 

Code: Select all

#NoEnv
#SingleInstance, Force

Loop, Parse, % "9, 59, 599, 3599, 35999, 86399, 863999", `,, %A_Space%
  MsgBox,,% A_LoopField, % LTrim( FmtSecs(A_LoopField, "{1:}:{2:02}:{3:02}:{4:02}"), "0:" )
User avatar
elModo7
Posts: 189
Joined: 01 Sep 2017, 02:38
GitHub: elModo7
Location: Spain
Contact:

Re: FmtSecs() : Converts Seconds to Days,Hours,Minutes,Seconds

01 Sep 2020, 12:28

Useful! I had made my own for ms to hh:mm:ss for game spedrunning huds.
This will come in handy for other pusposes I'm sure!
:beer:
User avatar
SKAN
Posts: 853
Joined: 29 Sep 2013, 16:58

Re: FmtSecs() : Converts Seconds to Days,Hours,Minutes,Seconds

02 Sep 2020, 17:50

@elModo7 : Thanks for the feedback :)

Return to “Scripts and Functions”

Who is online

Users browsing this forum: No registered users and 29 guests