Compare 2 strings of time in C#

compare time in c#
how to compare current time in c#
how to compare two dates in string format in c#
compare datetime c# greater than
c# compare time of day
c# program to compare two dates
how to compare hours and minutes in c#
convert string to time c#

So I've got a string 00:00:15:185 which I need to tell is greater than 15 seconds.

Time format is HH:m:ss:FFF

This is clearly longer than 15 seconds but I can't compare it properly.

Current code is this:

value = "00:00:15:185";    
if (DateTime.Parse(value) > DateTime.Parse("00:00:15:000"){
    //do stuff

It's giving exceptions when I run it all the time and the program doesn't work when it should

Your string doesn't represent a time, but an amount of time. We have TimeSpan for that.

var value = "00:00:15:185";
if (TimeSpan.ParseExact(value, @"hh\:mm\:ss\:FFF", CultureInfo.InvariantCulture)
        > TimeSpan.FromSeconds(15)) 
    //do stuff

none How to compare time which is in string format in c#? RRS feed, i am working on one app which will compare two different time. According to comparison it will do further task. i am taking both time in strings  C# String.Compare method compares two strings in C#. You can also use C# String.Equals method and StringComparer class and its method. This article and code examples demonstrate how to compare strings in C# using these different methods.

Another option(apart from @rob 's answer), use DateTime.ParseExact

    var value = "00:00:15:185";    

    if (DateTime.ParseExact(value, "HH:mm:ss:fff", CultureInfo.InvariantCulture) > 
        DateTime.ParseExact("00:00:15:000", "HH:mm:ss:fff", CultureInfo.InvariantCulture))
        // logic here.  

How to compare time which is in string format in c, The code sample in Listing 2 is an example of comparing two strings using String​.Compare method. string author1 = "Mahesh Chand";  Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more Compare 2 strings of time in C#

 DateTime time = DateTime.Now;
String result = time.ToString("HH:mm ");

 DateTime firstTimr = DateTime.ParseExact(reader["shift_start_time"].ToString(), "HH:mm:tt", null);
 String firstTimr1 = firstTimr.ToString("HH:mm ");
DateTime lastTime = DateTime.ParseExact(reader["Shift_last_time"].ToString(), "HH:mm:tt", null);
String lastTime1 = lastTime.ToString("HH:mm ");

if (DateTime.Parse(result) >= DateTime.Parse(firstTimr1) && (DateTime.Parse(result) <= DateTime.Parse(lastTime1)))
                       `enter code here` MessageBox.Show("First Shit");


How To Compare Strings In C#, This method is used to compare two TimeSpan values and returns an integer value t2: Specifies the second time interval that will be compared. Example 2​:. Compare. The static String.Compare method provides a thorough way of comparing two strings. This method is culturally aware. You can use this function to compare two strings or substrings of two strings. Additionally, overloads are provided that regard or disregard case and cultural variance.

DateTime.Compare() Method in C#, Hi all, I want to compare the system time with 11.00 AM on a button is pretty heavy because it has to take the string break it apart compare it  Depending on the requirement, C# offers a few ways to compare strings. For example, if you only require to check whether two strings are the same (ignoring case). Similarly, you may compare the strings based on linguistic and ordinal. In this tutorial of C# string comparison, I will show you examples of the following methods/classes:

TimeSpan.Compare() Method in C#, C#. Dear Sir I want to compare two dates which is in dd/MM/YYYY string Parse​()[^] method to convert the string to a DateTime value and then Simply convert the strings to DateTime objects, and then use the == operator to compare them: Substring(3, 2))); DateTime dtStartDate = new DateTime(int. String equality becomes complicated because there's a bunch of different ways to compare strings. See String.Equals and the StringComparison enum. None of that is slower than what's reasonable for what you're asking for, but if you're doing a culture-sensitive comparison when what you really mean is "do these two strings have the same bytes", you're being slower than you need to.

How to compare Time in C#?, The String class has a number of methods for comparing strings and portions of strings. boolean startsWith(String prefix, int offset), Considers the string beginning at steps through the string referred to by searchMe one character at a time. You can use DateTime.Parse() method to convert the string to a DateTime value and then after that, the compare is pretty straightforward. Permalink Posted 1-Aug-11 1:45am

  • Possible duplicate from this?… - better use DateTime.Compare or TimeSpan.Compare.
  • Thank you. I didn't know of that and it turns out it was only partly this at fault but another issue. So thank you for helping me work out where the fault was.
  • Welcome to Stack Overflow! While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.