Home > Net >  Unix Timestamp to HH-mm-ss
Unix Timestamp to HH-mm-ss

Time:02-16

I'm converting UnixTimeSpan to Date, but I want to display just the Hours-Minutes-Seconds of it. I'm trying with:

Public Shared Function UnixTimeStampToDateTime(ByVal unixTimeStamp As Double) As DateTime
        Dim dtDateTime As System.DateTime = New DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc)
        dtDateTime = dtDateTime.AddSeconds(unixTimeStamp).ToLocalTime()
              Return dtDateTime
    End Function

which return a correct datetime

02/15/2022 20:23:24

but when I'm trying to display just The Hours-Minutes-seconds with:

 RichTextBox1.Text= UnixTimeStampToDateTime(CDbl(unxTS.tostring("HH:mm:ss")))

I'm getting the error:

System.InvalidCastException: 'Conversion from string "HH:mm:ss" to type 'Double' is not valid.'

What is the problem?

CodePudding user response:

Passing the format into the wrong place. Try

RichTextBox1.Text = UnixTimeStampToDateTime(CDbl(unxTS)).ToString("HH:mm:ss")

because unxTS is a numeric (I guess), not a datetime. The format string applies to datetime, which is returned by your function.

  • Related