C# 当使用秒表和TimeSpan时,我怎样才能在点后得到没有数字的时分秒?
我有两个全局变量:C# 当使用秒表和TimeSpan时,我怎样才能在点后得到没有数字的时分秒?,c#,.net,C#,.net,我有两个全局变量: private Stopwatch stopwatch = new Stopwatch(); public static TimeSpan time = new TimeSpan(); 然后,我点击一个按钮启动秒表,然后点击另一个按钮停止秒表并执行以下操作: stopwatch.Stop(); time = stopwatch.Elapsed; 然后在表格1中: return testclass.time; public string SendResponse(Http
private Stopwatch stopwatch = new Stopwatch();
public static TimeSpan time = new TimeSpan();
然后,我点击一个按钮启动秒表,然后点击另一个按钮停止秒表并执行以下操作:
stopwatch.Stop();
time = stopwatch.Elapsed;
然后在表格1中:
return testclass.time;
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;
}
}
}
}
问题是在时间上,我得到的是00:00:00格式,但例如在秒中,我得到的是:00:00:05.434455我还没有检查分钟和小时,但如何使格式00:00:00在小时分和秒的点后没有数字/数字
所以我想及时得到的是例如:00:00:05或12:23:12或09:04:01
完整的数字,点后不带数字
time = TimeSpan.FromSeconds(Math.Round(time.TotalSeconds));
这是新类中的完整代码:
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";
}
}
在第二个事件中,我启动秒表。在上一个事件中,我停止秒表,并将秒表的“已用时间”属性指定给时间
然后在表格1中:
return testclass.time;
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.545
我想删除54545点之后的数字,因此我将只输入varr2“00:00:05”
或者是小时数,然后是“12:04:05”或任何其他情况,但带有完整的数字,但点后没有数字。只需使用格式为的Tostring方法即可。
time = TimeSpan.FromSeconds(Math.Round(time.TotalSeconds));
比如
时间字符串(@“hh:mm:ss”)
输出如下:00:00:05
有关详细信息,请查看您的完整代码。@HarimAbdu小时和分钟始终是整数。它仅取决于您的字符串格式。请试着把结果写在这里。