How to Convert a Timespan to a Formatted String

How do I convert a TimeSpan to a formatted string?

Would TimeSpan.ToString() do the trick for you? If not, it looks like the code sample on that page describes how to do custom formatting of a TimeSpan object.

TimeSpan to Custom string like HH:mm:ss

Just use :

delta.ToString(); // 00:00:10

which does the trick for you (Fiddle)

Or try this:

var str = string.Format("{0:00}:{1:00}:{2:00}", delta.Hours, delta.Minutes, delta.Seconds);

You can build an extension method if you use it a lot:

public static class MyExtensions
{
public static string ToCustomString(this TimeSpan span)
{
return string.Format("{0:00}:{1:00}:{2:00}", span.Hours, span.Minutes, span.Seconds);
}
}

Usage:

string strSpan = delta.ToCustomString();

How to format TimeSpan to string before .NET 4.0

One way could be:

TimeSpan ts = DateTime.Now - DateTime.Now.AddHours(-10);
Console.WriteLine(string.Format("{0:00}:{1:00}:{2:00}", ts.TotalHours, ts.Minutes, ts.Seconds));

Result would be something like:

09:59:59

EDIT:

Or you can try:

TimeSpan ts = DateTime.Now - DateTime.Now.AddHours(-10);
DateTime mydate = new DateTime(ts.Ticks);
Console.WriteLine(mydate.ToString(("hh:mm:ss")));

Output would be:

09:59:59

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

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

how to achieve timespan to string conversion?

Try using

DateTime d = new DateTime(time_span.Ticks);
string time = d.ToString("HH:mm:ss");

How to format this string of type TimeSpan

As per the documentation page entitled Custom TimeSpan format strings:

  • We can parse a string to a TimeSpan using TimeSpan.Parse(value)
  • We can use %h to get the hour component.
  • We can use 'h' to add the literal text "h".

Putting that together:

string formatted = TimeSpan.Parse("09:00").ToString("%h'h'"); // 9h

Note that TimeSpan.Parse("09:00") will only work if your culture is able to parse the timespan in this format. It might be safer to pass in a culture value to an appropriate overload of TimeSpan.Parse. For example:

TimeSpan.Parse("09:00", System.Globalization.CultureInfo.InvariantCulture);

Convert time span value to format hh:mm Am/Pm using C#

You can do this by adding your timespan to the date.

TimeSpan timespan = new TimeSpan(03,00,00);
DateTime time = DateTime.Today.Add(timespan);
string displayTime = time.ToString("hh:mm tt"); // It will give "03:00 AM"

How to convert a custom string representing a timespan into another format?

Just call ToString() on your $result according to your needs:

function Convert-TimeString ([String]$Time, [String[]]$Format)
{
$result = New-Object TimeSpan

$convertible = [TimeSpan]::TryParseExact(
$Time,
$Format,
[System.Globalization.CultureInfo]::InvariantCulture,
[ref]$result)

if ($convertible) { $result.ToString('hh\:mm\:ss\.fff') }
}

Convert-TimeString -Time '2M37.526S' -Format 'm\Mss\.fff\S'

Output:

00:02:37.526

Converting TimeSpan to hh:mm higher than 23:59

You won't be able to format a TimeSpan object via .ToString(format) overload the way you'd like.

Look at Microsoft doc, hh format cannot exceed 23 :

hh The number of hours, which ranges from "00" to "23".

You will have to build the format you want using .TotalHours.

What about:

var ts = new TimeSpan(hours: 50, minutes: 10, seconds: 20);
var hours = Math.Truncate(ts.TotalHours);
var formatted = $"{hours}:{ts.Minutes}:{ts.Seconds}";
Console.WriteLine(formatted); // Display 50:10:20

You could even create an extension method.



Related Topics



Leave a reply



Submit