Tag Archives: TimeSpan

Make a stopwatch in C#

This program is a simple stopwatch. When you click the Start button, the program changes the button’s caption to Stop and starts a Timer. As the stopwatch Timer runs, the program displays the elapsed time. Click the Stop button to … Continue reading

Posted in internationalization, miscellany, strings | Tagged , , , , , , , , , , , , , | Leave a comment

Add a ToString extension method to the TimeSpan structure in C#

Until .NET Framework version 4.0, you could not pass the TimeSpan structure’s ToString method a format string, so the result was always in the form hh:mm:ss.fffffff where hh is hours, mm is minutes, ss is seconds, and fffffff is 7 … Continue reading

Posted in extension methods, parsing | Tagged , , , , , , , , , , | 2 Comments

Find elapsed time in years, months, days, hours, minutes, and seconds in C#

The TimeSpan structure represents an elapsed time. It has Days, Hours, Minutes, Seconds, and Milliseconds properties that tell you about the time that has passed. For example, if two dates are 3 days and 4 hours apart, then Days = … Continue reading

Posted in parsing, variables | Tagged , , , , , , , , , , , , , , , , , | Leave a comment

Find the days, hours, minutes, or seconds between two dates in C#

This example uses a TimeSpan to find the days, hours, minutes, or seconds between two dates. When the user changes the date text, the program executes the following code. // If the text is a date, display // the elapsed … Continue reading

Posted in parsing, variables | Tagged , , , , , , , , , , , , , | Leave a comment