C# Time

Array Class Collections File Keyword String .NET ASP.NET Cast Compression Data Delegate Directive Enum Exception If Interface LINQ Loop Method Number Process Property Regex Sort StringBuilder Struct Switch Time Windows WPF


Time is a dimension.
Not an object,
not material,
it cannot be captured. Many objects exist that represent time. We use DateTime and TimeSpan.


DateTime. This type represents a point in time. Its date must be valid. The DateTime constructor will throw an exception if the date is invalid.

String face

Parse converts a string to its equivalent DateTime. Several parsing methods are available—they include Parse, ParseExact, TryParse and TryParseExact.

Parse, ParseExactTryParse, TryParseExactDateTime.Parse, SQLFromOADate

Format. We format DateTime instances when we want to store the DateTime in another representation, such as text. Humans read text, not bytes in structs.

DateTime FormatFilename, DateTimepubDate, RSS
About part

Advanced formats. We show how to format DateTime values in more complex ways. We format DateTimes in a "pretty" way. And we deal with 24-hour time formats.

Pretty Date24-Hour Time Formats
The current time: now

Now is my favorite time. It is so immediate. The Now property returns the current date and time on the system. Similarly, the Today property returns the current day with no time.


Months, days. These examples deal with using months and days. We access the Month property. We get all of the days of the week. And we store arrays of all months and days.

MonthDayOfWeekMonth, Day Arrays

Subtract. When we subtract dates, we can end up with a negative time period. But often we find the difference between a later date and an earlier one.

SubtractDays Elapsed
Null keyword

Null. We can never have a null DateTime instance. The DateTime is a value type, not a reference type, but we can use a nullable DateTime. We can also special-case MinValue.

MinValueNullable DateTime

TimeSpan. This type represents a period of time, not a specific point in time. When we subtract one DateTime from another, we get a TimeSpan instance.

TimeSpanTimeSpan to Long

TimeZone. Continuing on, you can easily access information about time zones. This is exciting stuff. Sadly TimeZone is not used in most programs.


Timer. With Timer we create a recurrent event based on an interval of time. The Timer type is useful for monitoring long-running processes such as websites.


Stopwatch. If we need to benchmark a program or part of one, we use the Stopwatch type. For some types of programs, we can use the Stopwatch type to monitor the startup time.


Algorithms. Once I complete my time machine I will post it here. Until then, this section covers some unusual ways to use DateTime. We optimize DateTime.

Closest DateDateTime PerformanceSort DateTimes

Relativity. In a classic programming book, we learn that threading in programs mirrors the Theory of Relativity. What is going on here?


Time is a complex subject. But with DateTime and TimeSpan we represent it with (relative) ease in our programs.
These types,
implemented as structs,
are value types,
not reference types.