kenjiro jaucian
kenjiro jaucian

Reputation: 451

VB.net Converting integer to desired format

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

Answers (1)

Tetsuya Yamamoto
Tetsuya Yamamoto

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

Related Questions