DateTime
Programming
Date Extraction
Time Extraction
Coding Tips

Getting Date or Time only from a DateTime Object

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Getting only the date or only the time from a DateTime value sounds simple, but in .NET there are several ways to do it depending on whether you need a new value, a string, or a modern type such as DateOnly or TimeOnly. The right method depends on what you are actually trying to preserve. A formatting problem and a data-modeling problem are not the same thing.

If You Need the Date Portion as a DateTime

Use the Date property.

csharp
1DateTime timestamp = new DateTime(2026, 3, 7, 14, 45, 30);
2DateTime dateOnlyAsDateTime = timestamp.Date;
3
4Console.WriteLine(dateOnlyAsDateTime);

Date returns a new DateTime whose time component is midnight. That means you still have a DateTime, not a special “date only” type.

This is useful when an API still expects DateTime but you want the time portion normalized away.

If You Need the Time Portion

Use TimeOfDay, which returns a TimeSpan.

csharp
1DateTime timestamp = new DateTime(2026, 3, 7, 14, 45, 30);
2TimeSpan timeOnly = timestamp.TimeOfDay;
3
4Console.WriteLine(timeOnly);

This is the correct choice when you want the duration since midnight, not a formatted string.

Formatting Versus Extracting

Sometimes developers say “I want only the date” when they really mean “I want to display only the date.”

Formatting example:

csharp
1DateTime timestamp = DateTime.Now;
2
3string dateText = timestamp.ToString("yyyy-MM-dd");
4string timeText = timestamp.ToString("HH:mm:ss");
5
6Console.WriteLine(dateText);
7Console.WriteLine(timeText);

This does not change the original value. It only changes how it is rendered.

That is why it is important to ask whether the output should be:

  • a new value for computation,
  • or just a string for display.

Use DateOnly and TimeOnly in Modern .NET

In .NET 6 and later, DateOnly and TimeOnly are often a better model when the domain truly contains only a date or only a time.

csharp
1DateTime timestamp = new DateTime(2026, 3, 7, 14, 45, 30);
2
3DateOnly dateOnly = DateOnly.FromDateTime(timestamp);
4TimeOnly timeOnly = TimeOnly.FromDateTime(timestamp);
5
6Console.WriteLine(dateOnly);
7Console.WriteLine(timeOnly);

These types are clearer than overloading DateTime for concepts that do not actually include both parts.

Time Zone and Offset Considerations

If time zone or offset matters, DateTimeOffset may be safer than DateTime.

csharp
1DateTimeOffset ts = new DateTimeOffset(2026, 3, 7, 14, 45, 30, TimeSpan.FromHours(-5));
2
3DateTimeOffset dateNormalized = new DateTimeOffset(ts.Date, ts.Offset);
4TimeSpan timeOnly = ts.TimeOfDay;

Be careful here: extracting the date after converting time zones can change the calendar day. That matters in scheduling, billing, and reporting systems.

A Good Rule of Thumb

Use:

  • 'Date when you need a DateTime at midnight,'
  • 'TimeOfDay when you need a TimeSpan,'
  • format strings when you only need display output,
  • 'DateOnly and TimeOnly when the domain really is date-only or time-only.'

This separates storage, computation, and presentation concerns cleanly.

Common Pitfalls

  • Using string formatting when the rest of the code actually needs a typed date or time value.
  • Assuming Date removes time zone concerns when the underlying interpretation still depends on the original value.
  • Treating TimeOfDay as a clock type instead of the TimeSpan it actually returns.
  • Keeping date-only business values in DateTime forever when DateOnly would express the intent better.
  • Forgetting that formatting a DateTime does not mutate the original value.

Summary

  • Use Date to get the date portion as a new DateTime with midnight time.
  • Use TimeOfDay to get the time portion as a TimeSpan.
  • Use formatting only when the goal is display, not data modeling.
  • Prefer DateOnly and TimeOnly in modern .NET when the domain actually needs them.
  • Be careful with time zones and offsets when extracting dates from timestamped values.

Course illustration
Course illustration

All Rights Reserved.