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 C#

2 Answers

0 votes
using System;

class Program
{
    static void Main() {
        string str = "12:14:36";

		List<string> time_parts = new List<string>();
		int pos = 0;

		while ((pos = str.IndexOf(":", pos, StringComparison.Ordinal)) != -1) {
			time_parts.Add(str.Substring(0, pos));
			str = str.Substring(pos + 1);
		}
		time_parts.Add(str); // Add the seconds

		if (time_parts.Count != 3) {
			Console.WriteLine("Invalid time format");
		}

		int hours = int.Parse(time_parts[0]);
		int minutes = int.Parse(time_parts[1]);
		int seconds = int.Parse(time_parts[2]);

		Console.WriteLine(hours + ":" + minutes + ":" + seconds);
    }
}





/*
run:
 
12:14:36
 
*/

 



answered Dec 27, 2023 by avibootz
edited Dec 27, 2023 by avibootz
0 votes
using System;

public class MyClass
{
	public static void Main(string[] args)
	{
		string str = "11:58:35";

		string[] arr = str.Split(':');

		int hours = int.Parse(arr[0].Trim());
		int minutes = int.Parse(arr[1].Trim());
		int seconds = int.Parse(arr[2].Trim());

		Console.WriteLine(hours + ":" + minutes + ":" + seconds);
	}
}



/*
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 162 views
2 answers 114 views
...