Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
katycorp
Visitor

Time Formatting

Hi there,
I have a quick question about time formatting. I'm trying to display a roDatetime with just the time formatting, but that doesn't seem to be one of the four options available to the asDateString() function. So instead I'm using the getHours() and getMinutes() functions to create it myself (doing my own check to convert the 24 hour clock to am/pm). However, when I print out the return value for these functions in the terminal, or explicitly convert them to strings for use in the short description field, there is always a space before the actual integer. Is this a bug or expected behavior?

Thanks,
Kaitlin
0 Kudos
3 REPLIES 3
TheEndless
Channel Surfer

Re: Time Formatting

It's expected, and is actually documented somewhere in the docs. I believe it's a placeholder for a sign. If you use the .ToStr() method instead, it won't add the space. You can also use the .Trim() method on the string to strip the white space.
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
TheEndless
Channel Surfer

Re: Time Formatting

Also, if you're interested, here are two utility functions I use for converting seconds to elapsed time strings. They could be fairly easily modified to return 12h or 24h time strings.

This one returns a string in the Roku time format (ex. 1h 22m 13s)...
Function GetDurationString( TotalSeconds = 0 As Integer ) As String
datetime = CreateObject( "roDateTime" )
datetime.FromSeconds( TotalSeconds )

hours = datetime.GetHours().ToStr()
minutes = datetime.GetMinutes().ToStr()
seconds = datetime.GetSeconds().ToStr()

duration = ""
If hours <> "0" Then
duration = duration + hours + "h "
End If
If minutes <> "0" Then
duration = duration + minutes + "m "
End If
If seconds <> "0" Then
duration = duration + seconds + "s"
End If

Return duration
End Function

And this one returns a string in a more traditional format (ex. 01:22:13)...
Function GetDurationStringStandard( TotalSeconds = 0 As Integer ) As String
datetime = CreateObject( "roDateTime" )
datetime.FromSeconds( TotalSeconds )

hours = datetime.GetHours().ToStr()
minutes = datetime.GetMinutes().ToStr()
seconds = datetime.GetSeconds().ToStr()

If Len( hours ) = 1 Then
hours = "0" + hours
End If
If Len( minutes ) = 1 Then
minutes = "0" + minutes
End If
If Len( seconds ) = 1 Then
seconds = "0" + seconds
End If

If hours <> "00" Then
Return hours + ":" + minutes + ":" + seconds
Else
Return minutes + ":" + seconds
End If
End Function
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
katycorp
Visitor

Re: Time Formatting

Ah that makes sense. I didn't realize there was a different implementation for Str() and toStr(). Thanks a bunch!
0 Kudos
Need Assistance?
Welcome to the Roku Community! Feel free to search our Community for answers or post your question to get help.

Become a Roku Streaming Expert!

Share your expertise, help fellow streamers, and unlock exclusive rewards as part of the Roku Community. Learn more.