处理负时间跨度

发布于 2024-07-24 16:40:07 字数 578 浏览 6 评论 0原文

在网格的输出中,我计算 TimeSpan 并获取其 TotalHours。 例如,

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

目标是将 TotalHours 显示为 39:44,因此我需要将值从 7.5 转换为 07:30。 这没问题......除非它是负数!

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

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

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

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

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

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

I can create a TimeSpan object from Hours with

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

负佳期 2024-07-31 16:40:07

没有 TimeSpan .Duration 方法? 我认为这可以解决您想要做的事情。

返回一个新的 TimeSpan 对象,其值是当前 TimeSpan 对象的绝对值。

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

Returns a new TimeSpan object whose value is the absolute value of the current TimeSpan object.

全部不再 2024-07-31 16:40:07
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"

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

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.

剧终人散尽 2024-07-31 16:40:07

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

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

玻璃人 2024-07-31 16:40:07

TimeSpan 类中有一个 Negate 方法。

MSDN 文档链接:
TimeSpan.Negate 方法()

There is a Negate method in the TimeSpan class.

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

ぃ弥猫深巷。 2024-07-31 16:40:07

简单的解决方案是:

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

hours = Math.Abs(hours)

The simple solution would be to do:

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

hours = Math.Abs(hours)
天涯离梦残月幽梦 2024-07-31 16:40:07

它的工作。尝试这个

mytimespam.Negate();

its working .try this

mytimespam.Negate();

妖妓 2024-07-31 16:40:07

您好,我将其写入了我一直在编写的一些代码中,希望它有帮助

(结果)是一个 int 变量

(TimeSpan.FromMinutes(result)) (TimeSpan.FromMinutes(result)) < 时间跨度为零? "-" + TimeSpan.FromMinutes(结果).ToString(@"hh\:mm") : "" + TimeSpan.FromMinutes(结果).ToString(@"hh\:mm");

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

(results) is an int variable

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

萌梦深 2024-07-31 16:40:07

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

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

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);
}
生寂 2024-07-31 16:40:07
TimeSpan Diff = Date1 - Date2;

if ((int)Diff.TotalDays < 0) { // your code }
TimeSpan Diff = Date1 - Date2;

if ((int)Diff.TotalDays < 0) { // your code }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文