如何在没有秒的小数部分的情况下格式化TimeSpan [英] How to Format a TimeSpan Without the Decimal Part of the Seconds

查看:153
本文介绍了如何在没有秒的小数部分的情况下格式化TimeSpan的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我显示TimeSpan,我会收到类似


00:05:17.6217891


当我想看到的是


00:05:18


有没有简单的方法来获得这个输出?尽我所能,我找不到

吧。


TIA


查尔斯

解决方案

这是我的想法....


dim TS as String =< GetTimeSpan>。 ToString

TS = TS.Substring(0,LastIndexOf(":)+ 2)


" Charles Law" < BL *** @ nowhere.com>在消息中写道

news:ef ************** @ TK2MSFTNGP15.phx.gbl ...

如果我显示TimeSpan我得到类似的东西:00:05:17.6217891

什么时候我想看到的是

00:05:18

有没有简单的方法来获得此输出?试着尽我所能,我找不到它。

TIA

Charles



Charles,

当我需要自定义格式化TimeSpan时,我通常做的是转换

它到DateTime,然后使用自定义DateTime格式。类似于:


Dim ts As TimeSpan

Dim dt As DateTime = DateTime.MinValue.Add(ts)

Dim s作为字符串


s = ts.ToString()''默认TimeSpan格式化

s = dt.ToString(" H:mm:ss")''custom DateTime格式化

有关自定义日期时间格式的详细信息,请参阅:

http://msdn.microsoft.com/library/de...matstrings.asp


有关.NET中格式化的信息,请参阅:
http://msdn.microsoft.com/library/de...ttingtypes.asp


-

Jay [MVP - Outlook]

..NET应用程序架构师,爱好者,&福音传教士

T.S.布拉德利 - http://www.tsbradley.net

" ; Charles Law < BL *** @ nowhere.com>在消息中写道

news:ef ************** @ TK2MSFTNGP15.phx.gbl ...

|如果我显示TimeSpan,我会收到类似

|

|的内容00:05:17.6217891

|

|当我想看到的是

|

| 00:05:18

|

|有没有一种简单的方法来获得这个输出?尽我所能,我找不到

|它。

|

| TIA

|

| Charles

|

|


Charles,

另外对于其他人来说,


我用:来表达各个部分。他们之间。


小时:分钟:秒钟


如果我很懒,那就是


dim str as string = ts.ToString.length(8)

(文档不太好)


我希望这有帮助,


Cor


If I display a TimeSpan I get something like

00:05:17.6217891

when what I would like to see is

00:05:18

Is there an easy way to get this output? Try as I might I just can''t find
it.

TIA

Charles

解决方案

Here''s my thought....

dim TS as String = <GetTimeSpan>.ToString
TS=TS.Substring(0,LastIndexOf(":")+2)

"Charles Law" <bl***@nowhere.com> wrote in message
news:ef**************@TK2MSFTNGP15.phx.gbl...

If I display a TimeSpan I get something like

00:05:17.6217891

when what I would like to see is

00:05:18

Is there an easy way to get this output? Try as I might I just can''t find
it.

TIA

Charles



Charles,
What I normally do when I need custom formatting of a TimeSpan is "convert"
it to a DateTime, then use custom DateTime formatting. Something like:

Dim ts As TimeSpan
Dim dt As DateTime = DateTime.MinValue.Add(ts)
Dim s As String

s = ts.ToString() '' default TimeSpan formatting
s = dt.ToString("H:mm:ss") '' custom DateTime formatting
For details on custom datetime formats see:

http://msdn.microsoft.com/library/de...matstrings.asp

For information on formatting in .NET in general see:
http://msdn.microsoft.com/library/de...ttingtypes.asp

--
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net
"Charles Law" <bl***@nowhere.com> wrote in message
news:ef**************@TK2MSFTNGP15.phx.gbl...
| If I display a TimeSpan I get something like
|
| 00:05:17.6217891
|
| when what I would like to see is
|
| 00:05:18
|
| Is there an easy way to get this output? Try as I might I just can''t find
| it.
|
| TIA
|
| Charles
|
|


Charles,

In addition to the others,

I concatinate the individual parts with the ":" between them.

Hours:Minutes:Seconds

If I am lazy it is

dim str as string = ts.ToString.length(8)

(not so nice as documentation)

I hope this helps,

Cor


这篇关于如何在没有秒的小数部分的情况下格式化TimeSpan的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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