C# 将 TimeSpan 从格式“hh:mm:ss”转换为“hh:mm”

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

Convert TimeSpan from format "hh:mm:ss" to "hh:mm"

c#asp.nettextboxtimespan

提问by Alex

I want to show in a TextBox only hour and minutes

我只想在 TextBox 中显示小时和分钟

var test = dataRow.Field<TimeSpan>("fstart").ToString();  
//test ="08:00:00"  
var tb = (TextBox) gridViewRow.Cells[2].FindControl("fstart");
tb.Text = test;

how to show only hours and minutes "hh.mm"

如何只显示小时和分钟 "hh.mm"

采纳答案by Habib

You need to convert your data to TimeSpan and then use format:"hh\:mm"

您需要将数据转换为 TimeSpan,然后使用格式:"hh\:mm"

string test ="08:00:00";
TimeSpan ts = TimeSpan.Parse(test);
Console.Write(ts.ToString(@"hh\:mm"));

In your case:

在你的情况下:

var test = dataRow.Field<TimeSpan>("fstart").ToString(@"hh\:mm"));

Remember to escape the colon :

记住要避开冒号 :

You may see: Custom TimeSpan Format Strings

您可能会看到:自定义时间跨度格式字符串

回答by Danilo Vulovi?

You can use TimeSpan methods:

您可以使用 TimeSpan 方法:

ToString("hh':'mm")
// or
ToString(@"hh\:mm")

Also check all available formats here http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

还要在这里检查所有可用的格式http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

回答by IvoTops

var test = dataRow.Field<TimeSpan>("fstart").ToString("hh.mm");  
//test ="08:00"  
var tb = (TextBox) gridViewRow.Cells[2].FindControl("fstart");
tb.Text = test;

回答by Olivier Jacot-Descombes

There is no need to convert from hh.mm.ssto hh.mm. TimeSpanis stored as a number of ticks (1 tick == 100 nanoseconds) and has no inherent format. What you have to do, is to convert the TimeSpaninto a human readable string! This involves formatting. If you do not specify a format explicitly, a default format will be used. In this case hh.mm.ss.

无需从 转换hh.mm.sshh.mmTimeSpan存储为多个刻度(1 个刻度 == 100 纳秒)并且没有固有格式。您需要做的是将 转换TimeSpan为人类可读的字符串!这涉及格式化。如果您未明确指定格式,则将使用默认格式。在这种情况下hh.mm.ss

string formatted = timespan.ToString(@"hh\.mm");

Note: This overload of ToStringexists since .NET 4.0. It does not support date and time placeholder separator symbols! Therefore you must include them as (escaped) string literals.

注意:此重载ToString自 .NET 4.0 起就存在。它不支持日期和时间占位符分隔符!因此,您必须将它们包含为(转义)字符串文字。

The usual way of formatting strings seems not to work for some odd reason (tested with .NET 3.5). (It does not make any difference whether you escape the separator symbol or not):

由于某些奇怪的原因(使用 .NET 3.5 测试),格式化字符串的常用方法似乎不起作用。(是否转义分隔符没有任何区别):

var timespan = TimeSpan.FromSeconds(1234);
string formatted = String.Format("{0:hh.mm}", timespan); // ==> 00:20:34

However, you can construct the string like this

但是,您可以像这样构造字符串

string formatted =
    String.Format("{0:00}.{1:00}", Math.Floor(timespan.TotalHours), timespan.Minutes);

or starting with VS2015 / C# 6.0, using string interpolation:

或从 VS2015 / C# 6.0 开始,使用字符串插值:

string formatted = $"{Math.Floor(timespan.TotalHours):00}.{timespan.Minutes:00}";

回答by daniele3004

The previous solutions don't run if hours>24, try this solution if you have time in minutes very big

如果小时数> 24,则以前的解决方案不会运行,如果您有非常大的时间,请尝试此解决方案

int minutes = 159000;
TimeSpan t = new TimeSpan(0, minutes, 0);

String HOURS = Math.Round(t.TotalHours, 0).ToString();
if (HOURS.Length==1)
{
    HOURS = "0"+HOURS;
}


String MINUTES = t.Minutes.ToString();
if (MINUTES.Length == 1)
{
    MINUTES = "0" + MINUTES;
}


String RESULT = HOURS + ":" + MINUTES;

回答by Ravi Rajan

You can achieve this by:

您可以通过以下方式实现:

  var hhmm = TimeSpan.FromMinutes(minutes).ToString(@"hh\:mm")

回答by Mahi Uddin

I know this is a very old question. If anyone wants to show single-digit hours when your hours are a single digit then you can use

我知道这是一个非常古老的问题。如果有人想在您的小时数是个位数时显示个位数小时数,那么您可以使用

var hoursWithMinutes = TimeSpan.FromHours(hours).ToString(@"h\:mm")

This way, when your hours are double-digit I mean greater than 9 then it will be showing 10:00 something like that.

这样,当您的小时数为两位数时,我的意思是大于 9,那么它将显示 10:00 类似的内容。