0

I'm developing a web app which is the transition from a previous desktop versión based in visual basic 6. I have to display times within a support ticket system of when the ticket first arrived,was first handled etc.

I'm running into certain problems when time formating since I seem to have to use several different methods to get what I want depending on the input and now I'm confused as to when I should use each.

For example, when using a system DateNow to get an answer in Hours, minutes and seconds this works fine:

Strings.Format(Date.Now, "HH:mm:ss")

But I have string values within the database, that are stored in a certain way and I cannot modify the database to have a timestamp field which I would prefer to store it, with the time, minutes and seconds scrunched up together as in where 13h,57mins,20secs ends in a string "135720". If I try the same

Dim info as string="135720"
Strings.Format(val(info),"HH:mm:ss)

The answer I get is the actual formating "HH:mm:ss", not the 13:57:20 I expected. But if I use a transformed VB6 function in .net:

Microsoft.VisualBasic.Format(Val(Info), "##:##:##")

it works just fine. I also have to use the Microsoft.VisualBasic.Format when trying to fix badly stored strings with partial times such as "1357" or just "135" to turn it into "13:57:00" or "13:50:00"using a case select based on the length of the string.

but I really don't want to be using old programming language in what is supposed to be a an updated app just because I don't finally understand how the new works and I have looked extensively into the documentation, but am none the wiser.

I did look within th comunity but didn't really find the answer either in such questions as: Is there a way to programmatically convert VB6 Formatting strings to .NET Formatting strings?

A regular expression to validate .NET time format

Time format of datetime field in .net

Time format in asp.net page

Any help finding the new way I should be doing this and why would be greatly apreciated.

Thank You

MatSnow
  • 7,357
  • 3
  • 19
  • 31
Alan Warden
  • 188
  • 8

1 Answers1

1

You can use TimeSpan.

This Function will return a TimeSpan from your String-value.

Public Function GetTimeSpan(s As String)
    Dim span As TimeSpan
    s = s.Trim  'Trim spaces

    'Check if string only contains numbers and if length is valid, else throw exception
    If System.Text.RegularExpressions.Regex.IsMatch(s, "^[0-9]+$") AndAlso
       s.Length >= 3 AndAlso
       s.Length <= 6 Then

        s = s.PadRight(6, "0")
    Else
        Throw New Exception("Invalid String!")
    End If

    TimeSpan.TryParseExact(s, "hhmmss", Globalization.CultureInfo.InvariantCulture, span)

    Return span
End Function

And use it like this:

MessageBox.Show(GetTimeSpan("135700").ToString)
MessageBox.Show(GetTimeSpan("135").ToString)
MatSnow
  • 7,357
  • 3
  • 19
  • 31
  • Perfect, the world of Timespan is going to solve many issues pending. Thank you very much, this is exactly what I was looking for. Just a suggestion for whoever else uses it, trim your ingoing strings to dodge unexpected length results :) – Alan Warden Sep 05 '17 at 09:24
  • 1
    Added `Trim` and Regex-check for numbers only. – MatSnow Sep 05 '17 at 10:06
  • 1
    Again more usefull like this, I had been using the instr() and .replace functions which are far less potent than regex. Thank you – Alan Warden Sep 05 '17 at 11:30