c# - 时间跨度数据类型 C#

标签 c# datetime timespan

我有一个时间跨度值,它是两个日期时间值的。它是这样的:

myvalue = {41870.01:44:22.8365404} 

我还有一个代表另一个时间跨度值的字符串,它是这样的:

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

我想检查 myvalue 是否小于 ttime。这是我的尝试:

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

我的方法是否足够正确?我需要做其他事情吗?谢谢。

最佳答案

我能看到的唯一问题是 TimeSpan.Parse 限制为 24 小时。所以你需要这个解决方法:

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

除此之外,以这种方式比较两个时间跨度绝对没问题。

如果它可以包含两个或三个部分(有或没有小时),这应该更安全:

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]));

对于它的值(value),这是一个从头开始编写的方法,它通过允许两个或三个标记(hh、mm、ss 或 mm、ss)将字符串解析为 TimeSpan,并且还支持奇怪的格式,例如 100:100:100(100 小时 + 100 分钟 + 100 秒):

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);
}

用你的字符串、我的字符串和一个奇怪的字符串进行测试:

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

输出:

00:23:55
2.00:23:55
4.05:41:40

关于c# - 时间跨度数据类型 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25422064/

相关文章:

javascript - 如何禁用 ion-datetime 中的特定日期?

TimeSpan 到友好的字符串库 (C#)

c# - 从 HH :mm:ss string 获取 TimeSpan

c# - 以秒和分钟为单位转换计时器间隔

c# - 仅在传递整数时使用命名参数

c# - 使用 Linq 总结一个数字(并跳过其余部分)

c# - C#中标签高度和宽度的自动大小

python - 在 pandas 中处理转换为 DateTime 的时间值而无需手动迭代?

c# - 我面临着对 IEnumerable(LINQ) > List(RecyclerView) 的巨大性能影响

php - 为什么 DateTime 对象在我执行空操作之前不可用?