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 aTimeSpan
usingTimeSpan.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
"Invalid JSON Primitive" in Ajax Processing
Correct, Idiomatic Way to Use Custom Editor Templates with Ienumerable Models in ASP.NET MVC
What Does {0} Mean When Found in a String in C#
How to Determine a Mapped Drive's Actual Path
Detect Antivirus on Windows Using C#
Escape Invalid Xml Characters in C#
Detect Webbrowser Complete Page Loading
C#: Waiting for All Threads to Complete
Get Return Value from Stored Procedure
Detect If a Method Was Overridden Using Reflection (C#)
Using C# .Net Libraries to Check for Imap Messages from Gmail Servers
.Net Regex: What Is the Word Character \W
Relocating App.Config File to a Custom Path
What's the Difference Between Bitmap.Clone() and New Bitmap(Bitmap)