timespan.parse question

P

Peter Proost

Hi group, it's been a long time since the last time I've been here but I
have a question.

I'm working with timespan.parse for calculating a duration, I have to add
strings which are in the following format 15:36:12 ==> Hours, minutes,
seconds
but sometimes there is a string like 55:26:32 ==> which is correct cause
the production machine was busy for 55 hours, but the timespan.parse crashes
on this value because it expects 2.7:26:32. I've written the following
function to convert 55:26:32 to 2.7:26:32 but I would like to know if there
isn't a built in function for this in .net 2003

Private Function ConvertToReadableString(ByVal input As String) As String
Dim strParts(2) As String
Dim intDays As Integer
Dim intHours As Integer

strParts = input.Split(":"c)

If CInt(strParts(0)) > 24 Then
intDays = CInt(Math.Floor(CInt(strParts(0)) / 24))
intHours = CInt(strParts(0)) Mod 24
Return CStr(intDays) & "." & CStr(intHours) & ":" & strParts(1)
& ":" & strParts(2)
Else
Return input
End If


End Function


Thanks

Peter
 
P

Peter Proost

Hi Cor I understand what you want to say. I also get why .net thinks more
than 23 hours is wrong in a datetime, but in my opinion more than 23 hours
should be allowed in a timespan, but that's probably just because I need it
now ;-)

Thanks Peter
 
S

Stephany Young

Don't panic Peter. You are correct, the TimeSpan.Oarse method will spit it's
dummy on "55:26:32", but there is a constructor for TimeSpan that takes,
hours > 23, minutes > 60 and seconds > 60:

You still need to split the source and convert.to integers though:

Private Function ConvertToTimeSpan(ByVal input As String) As TimeSpan

Dim _parts As String() = input.Split(":"c)

Return New TimeSpan(Integer.Parse(_parts(0)), Integer.Parse(_parts(1)),
Integer.Parse(_parts(2)))

End Sub
 
P

Peter Proost

Thanks Stephany, I didn't try that because I overlooked it.

thanks for the help

Greetz Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top