Ad space (banner)
#๏ธโƒฃC# Lessons
Lesson 36 / 68

Building and Displaying Dates (Basic Year/Month/Day Operations)

In this lesson you will learn the basics of working with dates in C#, so you can build a date and find its day of the week. This is for anyone searching for "C# DateTime how to use".

The DateTime structure lets you assemble and work with a specific date. Reading the day of the week and changing the display format are both straightforward.

The sample code creates a date with new DateTime(2026, 8, 9) and formats it as "8/9/2026" with d.ToString("M/d/yyyy"). Notice how d.DayOfWeek gives the day of the week, which is then looked up in an array of names.

A common stumbling block for beginners is the format string itself. The string you pass to ToString() decides the layout, so you have to look up the specifier that matches what you want.

In real-world development, combining it with the TimeSpan type, which represents a duration, makes it easy to calculate differences in days or hours - useful for posting timestamps and deadline calculations alike.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run C# directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)