C# 处理负时间跨度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1018643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:40:00  来源:igfitidea点击:

Handle negative time spans

c#.netdatetimetimespan

提问by

In my output of a grid, I calculate a TimeSpanand take its TotalHours. e.g.

在我的网格输出中,我计算 aTimeSpan并取其TotalHours. 例如

(Eval("WorkedHours") - Eval("BadgedHours")).TotalHours

The goal is to show the TotalHoursas 39:44, so I need to convert the value from 7.5to 07:30. This is no problem... unless it's negative!

目标是显示TotalHoursas 39:44,所以我需要将值从 转换7.507:30。这没问题……除非它是负数!

I can create a TimeSpanobject from Hourswith

我可以创建一个TimeSpan从对象Hours

TimeSpan.FromHours( (Eval("WorkedHours") - Eval("BadgedHours")).TotalHours)

If it's negative, I can't convert it to a DateTimeto use the .ToString("HH:mm")method, and the TimeSpanobject does not support the format string.

如果它是负数,我不能将它转换为 aDateTime使用该.ToString("HH:mm")方法,并且该TimeSpan对象不支持格式字符串。

回答by Daniel A. White

Just multiply it by -1 or use an absolute value function.

只需将其乘以 -1 或使用绝对值函数。

回答by jvenema

The simple solution would be to do:

简单的解决方案是:

string format = "HH:mm";
if(hours < 0)
  format = "-" + format;

hours = Math.Abs(hours)

回答by SLaks

static string ToHMString(TimeSpan timespan) { 
    if (timespan.Ticks < 0) return "-" + ToHMString(timespan.Negate());

    return timespan.TotalHours.ToString("#0") + ":" + timespan.Minutes.ToString("00");
}

Console.WriteLine(ToHMString(TimeSpan.FromHours(3)));       //Prints "3:00"
Console.WriteLine(ToHMString(TimeSpan.FromHours(-27.75)));  //Prints "-28:45"

This will also work correctly if the timespan is longer than 24 hours.

如果时间跨度超过 24 小时,这也将正常工作。

回答by Eugeniu Torica

There is a Negate method in the TimeSpan class.

TimeSpan 类中有一个 Negate 方法。

Link to the MSDN documentation: TimeSpan.Negate Method()

链接到 MSDN 文档: TimeSpan.Negate Method()

回答by Jared

Isn't there a TimeSpan.Durationmethod? I think this would handle what you are trying to do.

不是有TimeSpan.Duration方法吗?我认为这可以处理你想要做的事情。

回答by Christian Howe MRDJDZ

Hi i worked this into a bit of code i have been writing, hope it helps

嗨,我把它变成了我一直在写的一些代码,希望它有帮助

(results) is an int variable

(results) 是一个 int 变量

(TimeSpan.FromMinutes(result)) < TimeSpan.Zero ? "-" + TimeSpan.FromMinutes(result).ToString(@"hh\:mm") : "" + TimeSpan.FromMinutes(result).ToString(@"hh\:mm");

(TimeSpan.FromMinutes(result)) < TimeSpan.Zero ? "-" + TimeSpan.FromMinutes(result).ToString(@"hh\:mm") : "" + TimeSpan.FromMinutes(result).ToString(@"hh\:mm");

回答by Deval Patel

its working .try this

它的工作。试试这个

mytimespam.Negate();

mytimespam.Negate();

回答by sajin k

I checked to every where.But i didnt get a correct answer that why i used this way to finish

我检查了每个地方。但我没有得到正确的答案,为什么我用这种方式完成

TimeSpan diff = actualout.Subtract(actualin);
 string a =(diff.ToString()).ToString();
if(a.Contains("-"))
 {        
 diff = new TimeSpan(0,0,0,0);
}

回答by Mohammed Hedach

TimeSpan Diff = Date1 - Date2;

if ((int)Diff.TotalDays < 0) { // your code }