Tag Archives: DateTime

Use the Stopwatch class to time a program in C#

To measure elapsed time, you can save the current time with DateTime.Now and then compare it to a later time to see how much time has elapsed. The Stopwatch class in the System.Diagnostics namespace makes this a bit easier. It’s … Continue reading

Posted in performance | Tagged , , , , , , , , , | Leave a comment

Determine whether a year is a leap year in C#

Detecting leap years is basically trivial because the DateTime data type provides an IsLeapYear method to do just this. The example uses the following code to display the leap years between the two entered dates. // List leap years between … Continue reading

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

Compare performance looping over DateTime and integer variables in C#

The example Find Friday the Thirteenths in C# doesn’t really worry about performance. It uses integers to loop over the dates within a range. For every year between the start and end dates, the code considers the 13th of each … Continue reading

Posted in performance, syntax | Tagged , , , , , , , , , , , , , , | Leave a comment

Find Friday the Thirteenths in C#

The following code shows how this example lists the Friday the Thirteenths between selected start and end dates. // List Friday the 13ths between the start and end dates. private void btnGo_Click(object sender, EventArgs e) { lstResults.Items.Clear(); // Get the … Continue reading

Posted in performance, syntax | Tagged , , , , , , , , , , , | 1 Comment

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

Get the current time from the NIST server in C#

This example, courtesy of Adam Benson, shows how you can get the current time from the NIST atomic clock server. (I’ve reformatted it slightly to make it fit better on the blog page.) The key to the program is the … Continue reading

Posted in miscellany, system | Tagged , , , , , , , , , , , , , , | 1 Comment

Use the DateTime class’s standard formatting methods in C#

The ToString and string.Format methods let you use formatting characters to format a DateTime value, but it’s not easy to remember all of the characters you might want to use. To make using standard formats easier, the DateTime class provides … Continue reading

Posted in formatting, strings | Tagged , , , , , , , , , , , , , , , | Leave a comment