I want to show in a TextBox only hour and minutes
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"
There is no need to convert from
hh.mm.ss
tohh.mm
.TimeSpan
is stored as a number of ticks (1 tick == 100 nanoseconds) and has no inherent format. What you have to do, is to convert theTimeSpan
into a human readable string! This is called formatting. If you do not specify a format explicitly, a default format will be used. In this casehh.mm.ss
.Note: This overload of
ToString
exists since .NET 4.0. It does not support date and time placeholder separator symbols! Therefore you must include them as (escaped) string literals.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):
However, you can construct the string like this
or starting with VS2015 / C# 6.0, using string interpolation:
You can use TimeSpan methods:
Also check all available formats here http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
The previous solutions don't run if hours>24, try this solution if you have time in minutes very big
You need to convert your data to TimeSpan and then use format:
"hh\:mm"
In your case:
Remember to escape the colon
:
You may see: Custom TimeSpan Format Strings
You can achieve this by: