Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare 2 strings of time in 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

like image 636
Clarkinator Avatar asked Mar 12 '23 00:03

Clarkinator


1 Answers

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
}
like image 126
Rob Avatar answered Apr 21 '23 05:04

Rob