Forum Discussion

katycorp's avatar
katycorp
Visitor
15 years ago

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

3 Replies

  • 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.
  • 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
  • Ah that makes sense. I didn't realize there was a different implementation for Str() and toStr(). Thanks a bunch!