【问题标题】:When using StopWatch and TimeSpan how can i get hours minutes and seconds without numbers after the point?使用 StopWatch 和 TimeSpan 时,如何在点后获得没有数字的小时分钟和秒?
【发布时间】:2015-09-16 14:07:32
【问题描述】:

我有两个全局变量:

private Stopwatch stopwatch = new Stopwatch();
public static TimeSpan time = new TimeSpan();

然后我在一个按钮点击中启动秒表,在另一个按钮点击我停止它并做:

stopwatch.Stop();
time = stopwatch.Elapsed;

然后在form1中:

return testclass.time;

问题是我得到 00:00:00 格式的时间,但例如在第二个我得到:00:00:05.434455 我还没有检查分钟和小时但是我如何制作格式00:00:00 时分秒后不会有数字/数字?

所以我想及时获得例如:00:00:05 或 12:23:12 或 09:04:01 点后不带数字的完整数字。

这是一个新类的完整代码:

public static TimeSpan time = new TimeSpan();
 private Stopwatch stopwatch = new Stopwatch();

        private void videosInsertRequest_ResponseReceived(Video obj)
        {
            uploadstatus = obj.Status.UploadStatus;
            if (uploadstatus == "uploaded")
            {
                stopwatch.Stop();
                var milliseconds = stopwatch.ElapsedMilliseconds;
                time = stopwatch.Elapsed;
                uploadstatus = "file uploaded successfully";
            }
            if (uploadstatus == "Completed")
            {

            }
        }

        private void videosInsertRequest_ProgressChanged(IUploadProgress obj)
        {
            stringProgressReport[1] = obj.Status.ToString();
            if (stringProgressReport[1] == "Uploading")
            {
                stopwatch.Start();
                uploadstatus = "uploading file";
            }
        }

在第二个事件中,我在上一个事件中启动秒表,我停止秒表并将秒表的 Elapsed 属性分配给时间。

然后在form1中:

public string SendResponse(HttpListenerRequest request)
        {
            string result = "";
            string key = request.QueryString.GetKey(0);
            if (key == "cmd")
            {
                if (request.QueryString[0] == "uploadstatus")
                {
                    switch (Youtube_Uploader.uploadstatus)
                    {
                        case "file uploaded successfully":
                            Youtube_Uploader.uploadstatus = "";
                            return "upload completed," + Youtube_Uploader.fileuploadpercentages + ","
                               + test.time;
                   }
               }
             }
         }

然后在 form1 中,我将时间变量作为字符串的一部分返回。 然后在我程序的其他地方,我正在使用时间变量。

问题就像我上面描述的那样,结果我及时得到了。

if (textforspeech.contains("upload completed"))
                                {
                                    String[] parts = textforspeech.split(",");
                                    String varr = parts[0];
                                    String varr1 = parts[1];
                                    String varr2 = parts[2];

在 varr2 中,我得到时间“00:00:00” 问题是如果不是 00:00:00 而是 00:00:05.54545 我想删除点 54545 之后的数字,所以我只会进入 varr2 "00:00:05"

或者是小时,然后是“12:04:05”或任何其他情况,但在点后没有数字的完整数字。

【问题讨论】:

  • 请出示您的完整代码。

标签: c# .net


【解决方案1】:

只需使用带有格式的 Tostring 方法。 如 time.ToString(@"hh:mm:ss")

输出将是这样的:00:00:05

详情请查看https://msdn.microsoft.com/en-us/library/ee372287(v=vs.110).aspx

【讨论】:

  • 它只取决于你的字符串格式。请尝试在此处写出结果。
【解决方案2】:
time = TimeSpan.FromSeconds(Math.Round(time.TotalSeconds));

【讨论】:

  • @HarimAbdu 小时和分钟总是整数。
猜你喜欢
  • 1970-01-01
  • 2021-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多