格式化负的时间跨度 [英] Formatting a negative TimeSpan

查看:168
本文介绍了格式化负的时间跨度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做的时间跨度在.NET中,偶尔的总和产生负向时间跨度一些数学。当我显示的结果我无法格式化,以列入负面指标。

I'm doing some math with the Timespans in .Net, and occasionally the sum results in a negative Timespan. When I display the result I am having trouble formatting it to include the negative indicator.

Dim ts as New Timespan(-10,0,0)

ts.ToString()

这将显示-10:00:00,这是很好的,但我不想表现如此尝试这样做的秒

This will display "-10:00:00", which is good but I don't want to show the seconds so tried this.

ts.ToString("hh\:mm")

这将返回10:00,并放弃了 - 从前面这是问题的关键所在。我目前的解决办法是这样的:

This returns "10:00" and has dropped the "-" from the front which is the crux of the issue. My current solution is this:

If(ts < TimeSpan.Zero, "-", "") & ts.ToString("hh\:mm")

但我希望只使用格式字符串来完成相同的。

but I was hoping to accomplish the same by using only the format string.

推荐答案

我用.net反射的TimeSpan.ToString(...),它确实看起来并不像它支持任何形式的负面$ P $的PFIX的自定义格式 - 所以我觉得你的运气时,它会得到它为你按上述方式工作。 :(

I've used .Net Reflector on TimeSpan.ToString(...) and it really doesn't look like it supports any form of negative prefix on custom formats - so I think you're out of luck when it comes to getting it to work for you as above. :(

这篇关于格式化负的时间跨度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆