Reputation: 451
Code
myObject.TotalTime = 745
Dim test As Integer = Integer.Parse(myObject.TotalTime)
Dim test2 As String = test.ToString("00:00:00")
Desired result : 7:45:00
Current result : "00:07:45"
anyone knows a more sufficient way to convert this? like 2lines of code or even 1 line with a use of casting and such if only possible?
Upvotes: 0
Views: 44
Reputation: 24957
You can use DateTime.ParseExact()
method and use h:mm:ss
format string like this:
myObject.TotalTime = 745
Dim test As Integer = Integer.Parse(myObject.TotalTime)
' since it may contains 4 digits (e.g. 1000),
' use 4 digits for parsing instead of 3
Dim test1 As DateTime = DateTime.ParseExact(test.ToString("0000"), "HHmm", CultureInfo.InvariantCulture)
Dim test2 As String = test1.ToString("h:mm:ss")
Result: 7:45:00
Working example: .NET Fiddle Demo
Related issue: Convert numbers to time?
Upvotes: 3