Convert TimeSpan from format "hh:mm:ss" to "hh:mm"
Asked Answered
I

7

51

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"

Insolvency answered 1/10, 2012 at 12:24 Comment(1)
A tip would be to have a look at Roslyn CTP which I've found very useful for testing formatting in C# if you are using Visual Studio.Splitting
C
104

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

Coolant answered 1/10, 2012 at 12:29 Comment(2)
If Same occurs in ASP .net there are no such methods like .Tostring() with argument acceptance.Exudate
@Rahul, overloaded TimeSpan.ToString methods were provided in .Net framework 4.0. I guess the ASP.Net application you are referring to is using a previous version of .Net framework.Coolant
S
16

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}";
Soliz answered 1/10, 2012 at 12:38 Comment(1)
Don't forget Math.Floor() on the TotalHours. Without this 29,7h => Gets converted to 30:42...Palmation
T
8

You can use TimeSpan methods:

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

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

Thereabouts answered 1/10, 2012 at 12:26 Comment(2)
TimeSpan.ToString doesn't support this formatting. see thisElora
This is new in .Net 4Shela
C
3
var test = dataRow.Field<TimeSpan>("fstart").ToString("hh.mm");  
//test ="08:00"  
var tb = (TextBox) gridViewRow.Cells[2].FindControl("fstart");
tb.Text = test;
Colicweed answered 1/10, 2012 at 12:27 Comment(1)
If the input contains the : , it will fail on fomrat hh.mmCoolant
M
2

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.

Malnourished answered 28/4, 2020 at 13:45 Comment(0)
C
1

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

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;
Coastline answered 20/12, 2016 at 8:43 Comment(0)
S
0

You can achieve this by:

  var hhmm = TimeSpan.FromMinutes(minutes).ToString(@"hh\:mm")
Siddra answered 16/4, 2018 at 10:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.