Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timespan data type C#

I have a timespan value which is difference of two Datetime values. It is like this :

myvalue = {41870.01:44:22.8365404} 

I have also a string which represents another timespan value and it is like this :

ttime= "23:55" // which means 23 minutes and 55 seconds

I want to check if myvalue is smaller than ttime. Here is my attempt:

if(myvalue < Timespan.Parse(ttime))
//do this
else
//do that

Is my approach correct enough? Do I need to do something else? Thanks.

like image 505
jason Avatar asked Dec 30 '25 17:12

jason


1 Answers

The only problem i can see is that TimeSpan.Parse is limited to 24 hours. So you would need this workaround:

string ttime = "48:23:55"; // 48 hours
TimeSpan ts = new TimeSpan(int.Parse(ttime.Split(':')[0]),    // hours
                           int.Parse(ttime.Split(':')[1]),    // minutes
                           int.Parse(ttime.Split(':')[2]));   // seconds

Apart from that it's absolutely fine to compare two timespan in this way.

If it can contain two or three parts(with or without hours), this should be safer:

string[] tokens = ttime.Split(':');
// support only two or three tokens:
if (tokens.Length < 2 || tokens.Length > 3)
    throw new NotSupportedException("Timespan could not be parsed successfully: " + ttime);
TimeSpan ts;
if(tokens.Length == 3)
    ts = new TimeSpan(int.Parse(tokens[0]), int.Parse(tokens[1]),int.Parse(tokens[2]));
else
    ts = new TimeSpan(0, int.Parse(tokens[0]), int.Parse(tokens[1]));

For what it's worth, here is a method which is written from scratch which parses a string to TimeSpan by allowing two or three tokens(hh,mm,ss or mm,ss) and also supports weird formats like 100:100:100 (100 hours + 100 minutes + 100 seconds):

public static TimeSpan SaferTimeSpanParse(string input, char delimiter = ':')
{
    if (string.IsNullOrEmpty(input))
        throw new ArgumentNullException("input must not be null or empty", "input");
    string[] tokens = input.Split(delimiter);
    if (tokens.Length < 2 || tokens.Length > 3)
        throw new NotSupportedException("Timespan could not be parsed successfully: " + input);
    int i;
    if (!tokens.All(t => int.TryParse(t, out i) && i >= 0))
        throw new ArgumentException("All token must contain a positive integer", "input");

    int[] all = tokens.Select(t => int.Parse(t)).ToArray();
    int hoursFinal = 0, minutesFinal = 0, secondsFinal = 0;

    int seconds = all.Last();
    secondsFinal = seconds % 60;
    minutesFinal = seconds / 60;

    int minutes = (all.Length == 3 ? all[1] : all[0]) + minutesFinal; // add current minutes which might already be increased through seconds
    minutesFinal = minutes % 60;
    hoursFinal = minutes / 60;

    hoursFinal = all.Length == 3 ? all[0] + hoursFinal : hoursFinal; // add current hours which might already be increased through minutes

    return new TimeSpan(hoursFinal, minutesFinal, secondsFinal);
}

tested with your string, my string and a weird one:

Console.WriteLine(SaferTimeSpanParse("23:55"));
Console.WriteLine(SaferTimeSpanParse("48:23:55"));
Console.WriteLine(SaferTimeSpanParse("100:100:100"));

Output:

00:23:55
2.00:23:55
4.05:41:40
like image 60
Tim Schmelter Avatar answered Jan 01 '26 06:01

Tim Schmelter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!