Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,895 questions

51,826 answers

573 users

How to extract hours, minutes and second from string in VB.NET

2 Answers

0 votes
Imports System
Imports System.Collections.Generic

Public Class Program
    Public Shared Sub Main()
        Dim str As String = "12:14:36"
        Dim time_parts As List(Of String) = New List(Of String)() From {}
        Dim pos As Integer = str.IndexOf(":", 0, StringComparison.Ordinal)

		While (pos <> -1)
            time_parts.Add(str.Substring(0, pos))
            str = str.Substring(pos + 1)
			pos = str.IndexOf(":", pos, StringComparison.Ordinal)
        End While
		
		time_parts.Add(str)
		
		If time_parts.Count <> 3 Then
            Console.WriteLine("Invalid time format")
        End If
	
		Dim hours As Integer = Integer.Parse(time_parts(0))
        Dim minutes As Integer = Integer.Parse(time_parts(1))
        Dim seconds As Integer = Integer.Parse(time_parts(2))
		
        Console.WriteLine(hours & ":" & minutes & ":" & seconds)

    End Sub
End Class



' run:
'
' 12:14:36
'

 



answered Dec 27, 2023 by avibootz
edited Dec 27, 2023 by avibootz
0 votes
Imports System

Public Class Program
    Public Shared Sub Main(ByVal args As String())
        Dim str As String = "11:58:35"
        Dim arr As String() = str.Split(":"c)
		
        Dim hours As Integer = Integer.Parse(arr(0).Trim())
        Dim minutes As Integer = Integer.Parse(arr(1).Trim())
        Dim seconds As Integer = Integer.Parse(arr(2).Trim())
		
        Console.WriteLine(hours & ":" & minutes & ":" & seconds)
    End Sub
End Class



' run:
'
' 11:58:35
'

 



answered Dec 28, 2023 by avibootz

Related questions

1 answer 140 views
1 answer 104 views
2 answers 152 views
2 answers 108 views
2 answers 114 views
...