C# 4.0: Can I use a TimeSpan as an optional parameter with a default value?

You can work around this very easily by changing your signature. void Foo(TimeSpan? span = null) { if (span == null) { span = TimeSpan.FromSeconds(2); } … } I should elaborate – the reason those expressions in your example are not compile-time constants is because at compile time, the compiler can’t simply execute TimeSpan.FromSeconds(2.0) and … Read more

Why does TimeSpan.ParseExact not work

From the documentation: Any other unescaped character in a format string, including a white-space character, is interpreted as a custom format specifier. In most cases, the presence of any other unescaped character results in a FormatException. There are two ways to include a literal character in a format string: Enclose it in single quotation marks … Read more

Format A TimeSpan With Years

A TimeSpan doesn’t have a sensible concept of “years” because it depends on the start and end point. (Months is similar – how many months are there in 29 days? Well, it depends…) To give a shameless plug, my Noda Time project makes this really simple though: using System; using NodaTime; public class Test { … Read more

What is the easiest way to subtract time in C#?

These can all be done with DateTime.Add(TimeSpan) since it supports positive and negative timespans. DateTime original = new DateTime(year, month, day, 8, 0, 0); DateTime updated = original.Add(new TimeSpan(5,0,0)); DateTime original = new DateTime(year, month, day, 17, 0, 0); DateTime updated = original.Add(new TimeSpan(-2,0,0)); DateTime original = new DateTime(year, month, day, 17, 30, 0); DateTime … Read more

Environment.TickCount vs DateTime.Now

Environment.TickCount is based on GetTickCount() WinAPI function. It’s in milliseconds But the actual precision of it is about 15.6 ms. So you can’t measure shorter time intervals (or you’ll get 0) Note: The returned value is Int32, so this counter rolls over each ~49.7 days. You shouldn’t use it to measure such long intervals. DateTime.Ticks … Read more

Measuring code execution time

A better way would be to use Stopwatch, instead of DateTime differences. Stopwatch Class – Microsoft Docs Provides a set of methods and properties that you can use to accurately measure elapsed time. // create and start a Stopwatch instance Stopwatch stopwatch = Stopwatch.StartNew(); // replace with your sample code: System.Threading.Thread.Sleep(500); stopwatch.Stop(); Console.WriteLine(stopwatch.ElapsedMilliseconds);

How do I convert a TimeSpan to a formatted string? [duplicate]

I just built a few TimeSpan Extension methods. Thought I could share: public static string ToReadableAgeString(this TimeSpan span) { return string.Format(“{0:0}”, span.Days / 365.25); } public static string ToReadableString(this TimeSpan span) { string formatted = string.Format(“{0}{1}{2}{3}”, span.Duration().Days > 0 ? string.Format(“{0:0} day{1}, “, span.Days, span.Days == 1 ? string.Empty : “s”) : string.Empty, span.Duration().Hours > … Read more

Format TimeSpan greater than 24 hour

Well, the simplest thing to do is to format this yourself, e.g. return string.Format(“{0}hr {1}mn {2}sec”, (int) span.TotalHours, span.Minutes, span.Seconds); In VB: Public Shared Function FormatTimeSpan(span As TimeSpan) As String Return String.Format(“{0}hr {1}mn {2}sec”, _ CInt(Math.Truncate(span.TotalHours)), _ span.Minutes, _ span.Seconds) End Function I don’t know whether any of the TimeSpan formatting in .NET 4 would … Read more