Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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"

like image 303
Alex Avatar asked Oct 01 '12 12:10

Alex


People also ask

How to get hh mm from TimeSpan in c#?

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

How do you convert TimeSpan to hours?

A TimeSpan value can be represented as [-]d. hh:mm:ss. ff, where the optional minus sign indicates a negative time interval, the d component is days, hh is hours as measured on a 24-hour clock, mm is minutes, ss is seconds, and ff is fractions of a second. The value of the Hours property is the hours component, hh.

What is the format of a TimeSpan?

"c" is the default TimeSpan format string; the TimeSpan. ToString() method formats a time interval value by using the "c" format string. TimeSpan also supports the "t" and "T" standard format strings, which are identical in behavior to the "c" standard format string.

What is TimeSpan C#?

C# TimeSpan struct represents a time interval that is difference between two times measured in number of days, hours, minutes, and seconds. C# TimeSpan is used to compare two C# DateTime objects to find the difference between two dates.


2 Answers

You need to convert your data to TimeSpan and then use format:"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

like image 70
Habib Avatar answered Oct 16 '22 17:10

Habib


There is no need to convert from hh.mm.ss to hh.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 the TimeSpan into 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.

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

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):

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:

string formatted = $@"{timespan:hh\:mm}"; 
like image 37
Olivier Jacot-Descombes Avatar answered Oct 16 '22 15:10

Olivier Jacot-Descombes