Working with DateTime

Vaibhav • September 11, 2025

Time is a part of almost every real-world program. Whether you're displaying today's date, tracking when something happened, or calculating how many days are left until an event, you need a way to represent and work with time. In C#, the built-in DateTime type gives you that ability. In this article, we’ll introduce DateTime as a concept and show how it fits into your journey as a beginner programmer - without using methods, properties, or advanced features.

What is DateTime?

DateTime is a type in C# that represents a specific moment in time. It includes both the date (like September 11, 2025) and the time (like 8:53 PM). You can think of it as a timestamp - a snapshot of "when" something happens.

Just like int is used for whole numbers and string is used for text, DateTime is used for time-related values. It is part of the C# language and available by default, so you don’t need to install anything or import any special libraries to use it.

Getting the current time

The most common way to use DateTime is to get the current date and time. C# provides a built-in value called DateTime.Now that gives you the exact moment your program is running.

DateTime now = DateTime.Now;
Console.WriteLine(now);

In this example, we declare a variable named now and assign it the current time. When printed, it might show something like 9/11/2025 20:53:00, depending on your system’s settings. This value includes both the date and the time.

You can use this value to mark when something happened, display the current time to the user, or calculate how much time has passed between two events.

DateTime is a value type, just like int or bool. It stores a complete timestamp in a single variable.

Storing a specific date

Sometimes you want to work with a specific date - like a birthday, a deadline, or a holiday. You can create a DateTime value that represents that moment.

DateTime birthday = new DateTime(1995, 12, 5);
Console.WriteLine(birthday);

This creates a DateTime value for December 5, 1995. The numbers inside the parentheses represent the year, month, and day - in that order. When printed, it shows the full date and time, with the time part set to midnight by default.

You can use this technique to store important dates in your program and compare them with other dates later.

Comparing two dates

Once you have two DateTime values, you can compare them using simple comparison operators like <, >, or ==. This helps you check whether one date is before or after another.

DateTime today = DateTime.Now;
DateTime deadline = new DateTime(2025, 9, 15);

if (today < deadline)
{
    Console.WriteLine("You still have time!");
}
else
{
    Console.WriteLine("Deadline has passed.");
}

In this example, we compare the current time with a fixed deadline. If today is earlier, we print a message saying there’s still time. Otherwise, we say the deadline has passed. This kind of logic is useful in scheduling and reminders.

Adding and subtracting days

You can perform simple arithmetic with DateTime values. For example, if you want to calculate a future date, you can add days using the + operator and a special type called TimeSpan.

DateTime today = DateTime.Now;
TimeSpan fiveDays = new TimeSpan(5, 0, 0, 0);
DateTime future = today + fiveDays;

Console.WriteLine(future);

This creates a new date that is five days ahead of today. The TimeSpan represents a duration - in this case, 5 days. You can subtract days in the same way by using the - operator.

DateTime past = today - fiveDays;
Console.WriteLine(past);

This gives you the date five days ago. These operations are useful for calculating due dates, expiration dates, or how long ago something happened.

DateTime values are immutable. That means when you add or subtract time, you get a new value - the original one doesn’t change.

Displaying a date in a message

You can use DateTime values in string interpolation to build dynamic messages. This helps personalize output and make it more informative.

DateTime now = DateTime.Now;
Console.WriteLine($"Today is {now}");

This prints the full date and time. While we haven’t introduced formatting yet, you can still use the value directly in your messages. Later, you’ll learn how to format it to show only the parts you want.

Using DateTime in simple programs

Let’s say you want to write a program that tells the user how many days are left until a specific event. You can do this by subtracting two DateTime values and storing the result.

DateTime today = DateTime.Now;
DateTime examDate = new DateTime(2025, 9, 30);
TimeSpan remaining = examDate - today;

Console.WriteLine("Days until exam:");
Console.WriteLine(remaining.Days);

This program calculates the number of days between today and the exam date. The result is stored in a TimeSpan, and you can access the number of days directly. This is a simple way to track deadlines and countdowns.

When working with time, always store your values in variables with clear names like today, deadline, or eventDate. This makes your code easier to read and understand.

Common mistakes and how to avoid them

One common mistake is assuming that DateTime values change when you add or subtract time. They don’t - you must store the result in a new variable.

DateTime today = DateTime.Now;
today + new TimeSpan(1, 0, 0, 0); // ❌ This does nothing

Console.WriteLine(today); // Still today

To fix this, store the result:

DateTime tomorrow = today + new TimeSpan(1, 0, 0, 0); // ✅

Another mistake is mixing up the order of values when creating a specific date. Remember: the order is year, month, day.

DateTime wrong = new DateTime(5, 12, 1995); // ❌ Wrong order
DateTime correct = new DateTime(1995, 12, 5); // ✅

You can use DateTime.Today to get the current date with the time set to midnight. This is useful when you only care about the date and not the time.

Summary

DateTime is a built-in type in C# that helps you work with dates and times. You learned how to get the current time using DateTime.Now, create specific dates, compare dates, and perform simple arithmetic using TimeSpan. You also saw how to use DateTime in messages and how to avoid common mistakes.

These skills help you build programs that are aware of time - whether you're logging events, scheduling tasks, or displaying friendly messages. As you continue learning C#, you’ll discover even more ways to work with time, including formatting, durations, and calendars. But for now, you have a solid foundation for using DateTime effectively.