C# 以毫秒为单位显示持续时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9565740/
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-09 07:55:01  来源:igfitidea点击:

Display duration in milliseconds

c#c#-4.0

提问by fn27

I want to display duration with milliseconds on a web page. So far I have done this: I managed to display this output on a label: 00:02:50, but I want to display milliseconds as well, so the result should look like this 00:02:50:000. How do I achieve this?

我想在网页上以毫秒显示持续时间。到目前为止,我已经这样做了:我设法在标签上显示此输出:00:02:50,但我也想显示毫秒,因此结果应该如下所示00:02:50:000。我如何实现这一目标?

Code behind:

后面的代码:

protected void Page_Load(object sender, EventArgs e)
{
    DateTime startTime = DateTime.Now;
    // sleep for 2.5s
    Thread.Sleep(2500);
    DateTime stopTime = DateTime.Now;
    TimeSpan duration = stopTime - startTime;
    Result.Text = duration.ToString("mm':'ss':'ff");
}

采纳答案by james lewis

First of all, if you're timing things I would recommend using the StopWatch class as that's what it's there for. You can find it in the System.Diagnostics namespace: System.Diagnostics.Stopwatch.

首先,如果您要计时,我建议您使用 StopWatch 类,因为这就是它的用途。您可以在System.Diagnostics命名空间中找到它:System.Diagnostics.Stopwatch

You can instantiate a new one and start measuring the elapsed amount of time with one line of code: var stop = System.Diagnostics.Stopwatch.StartNew();and then stop the timer with the stop method: stop.Stop();. You can then return the elapsed time using the Elapsed property var elapsed = stop.Elapsed;.

您可以实例化一个新的并使用一行代码开始测量经过的时间:var stop = System.Diagnostics.Stopwatch.StartNew();然后使用 stop 方法停止计时器:stop.Stop();。然后,您可以使用 Elapsed 属性返回经过的时间var elapsed = stop.Elapsed;

Then in order to display the elapsed time with milliseconds you would call the ToString method on the elapsed timespan with the correct parameters.

然后,为了以毫秒为单位显示经过的时间,您可以使用正确的参数在经过的时间跨度上调用 ToString 方法。

So putting it all together your code would look like this:

所以把它们放在一起你的代码看起来像这样:

protected void Page_Load(object sender, EventArgs e)
{
    var timer = System.Diagnostics.Stopwatch.StartNew();
    // sleep for 2.5s
    Thread.Sleep(2500);
    timer.Stop();
    var elapsed = timer.Elapsed;
    Result.Text = elapsed.ToString("mm':'ss':'fff");
}

Hope that helps!

希望有帮助!

James

詹姆士

回答by Candide

The doc says: "fff" gives you:

医生说:“FFF”为您提供:

The milliseconds in a date and time value.

日期和时间值中的毫秒数。

You're using "ff" which gives you:

您正在使用“ff”,它为您提供:

The hundredths of a second in a date and time value.

日期和时间值中的百分之一秒。

So, change your code to:

因此,将您的代码更改为:

duration.ToString("mm':'ss':'fff");

回答by svick

I think you're confused. In your case 00:02:50means 2 seconds and 50 hundredths of second. If you want to display milliseconds, use format like mm':'ss':'fff(notice the one added f). This will print something like 00:02:500, i.e. 2 seconds and 500 thousandths of second, or 2 s 500 ms.

我想你很困惑。在你的情况下00:02:50意味着 2 秒和百分之五十秒。如果您想显示毫秒,请使用类似的格式mm':'ss':'fff(注意添加的那个f)。这将打印类似00:02:500,即 2 秒和千分之 500 秒,或 2 秒 500 毫秒的内容。

But this doesn't mean your measurements will be precise down to millisecond.That's not what DateTime.Nowis meant to do. If you want to make measurements this precise, you should use StopWatch.

但这并不意味着您的测量将精确到毫秒。这不是DateTime.Now本意。如果您想进行如此精确的测量,您应该使用StopWatch.

回答by Abbas

Otherwise just use the properties from the timespan like this:

否则,只需使用时间跨度中的属性,如下所示:

var result = String.Format("{0}:{1}:{2}", duration.Minutes, duration.Seconds, duration.Milliseconds);
Result.Text = result

This way I think you gain more control over what you want to display, instead of formatting the timespan in the ToString()-method which more easily allows typos to be made...

通过这种方式,我认为您可以更好地控制要显示的内容,而不是在 ToString() 方法中格式化时间跨度,这样更容易出现拼写错误...

Hope this helps!

希望这可以帮助!

Update:To add the hours as well this is how it'll look like:

更新:要添加时间,这就是它的样子:

var result = String.Format("{0}:{1}:{2}:{3}", duration.Hours, duration.Minutes, duration.Seconds, duration.Milliseconds);

回答by raveturned

Your current code should be displaying minutes, seconds, and hundredths of a second.

您当前的代码应该显示分、秒和百分之一秒

Result.Text = duration.ToString("mm':'ss':'ff");

To display milliseonds instead of hundredths of a second:

要显示毫秒而不是百分之一秒:

// output: 00:02:500
Result.Text = duration.ToString("mm':'ss':'fff");

See the documentation for Custom Date and Time Format Strings.

请参阅自定义日期和时间格式字符串的文档。

回答by Niranjan Singh

Use TimeSpan.ToString Methodwith custom format.

使用自定义格式的TimeSpan.ToString 方法

The returned string is formatted with the "c" format specifier and has the following format:

返回的字符串使用“c”格式说明符进行格式化,并具有以下格式:

[-][d.]hh:mm:ss[.fffffff]

Elements in square brackets ([ and ]) may not be included in the returned string. Colons and periods (: and.) are literal characters.

方括号([ 和 ])中的元素可能不包含在返回的字符串中。冒号和句点(: 和.)是文字字符。

Result.Text = duration.ToString("mm:ss:fff");

or

或者

Result.Text = duration.ToString("hh:mm:ss.fff");

Ref :Custom Date and Time Format Strings, The "fff" Custom Format Specifier

参考:自定义日期和时间格式字符串“fff”自定义格式说明符

DateTime date1 = new DateTime(2008, 8, 29, 19, 27, 15, 18);
CultureInfo ci = CultureInfo.InvariantCulture;
Console.WriteLine(date1.ToString("hh:mm:ss.fff", ci));
// Displays 07:27:15.018