How to get time from DateTime in C#?

By Josip Miskovic
A thumbnail showing C# code on DateTime.

There are three ways to get time form DateTime in C#:

How to get time from DateTime in C#?

The best way to get time from DateTime in C# is to:

  1. Access the TimeOfDay property of DateTime to get TimeSpan value for your DateTime.
  2. Access the Hours property of TimeSpan to get the hour of the DateTime.
  3. Access the Minutes property of TimeSpan to get the minutes of the DateTime.
  4. Access the Seconds property of TimeSpan to get the seconds of the DateTime.

The code below shows how to get time from DateTime:

C#
TimeSpan currentTime = DateTime.Now.TimeOfDay; 
Console.WriteLine(currentTime); // 19:10:30.1729426
Console.WriteLine(currentTime.Hours); // 19
Console.WriteLine(currentTime.Minutes); // 10
Console.WriteLine(currentTime.Seconds); // 30

TimeOfDay property

The source code of the DateTime struct shows how the TimeOfDay property is calculated:

C#
// Returns the time-of-day part of this DateTime. The returned value
// is a TimeSpan that indicates the time elapsed since midnight.
//
public TimeSpan TimeOfDay {
    get {
        return new TimeSpan(InternalTicks % TicksPerDay);
    }
}

What is TimeSpan?

TimeSpan data type represents time of the day.

The key properties for getting time using the TimeSpan type are:

  • Hours - Gets the hours component of the time interval. From 0 to 23.
  • Minutes - Gets the minutes component of the time interval.
  • Seconds - Gets the seconds component of the time interval.

See the source code of TimeSpan for more details

Get time from DateTime using DateTime type

Alternative for getting time from DateTime is to use the DateTime type with date of 1/1/0001.

The idea is to ignore the date portion of the time.

The code below shows how to get time from current DateTime:

C#
DateTime timeOnly = new DateTime(DateTime.Now.TimeOfDay.Ticks);
Console.WriteLine(timeOnly); // 1/1/0001 7:10:30 PM

We can still access the hour, minutes, and seconds properties, but the intent is not as clear as using TimeSpan.

Get time from DateTime using ToString method

Another way to get only time from DateTime is to use ToString method:

C#
DateTime currentTime = DateTime.Parse("6/19/2022 12:34:12 AM");

Console.WriteLine(currentTime.ToString("HH:mm")); // 00:34
Console.WriteLine(currentTime.ToString("hh:mm tt")); // 12:34 AM
Console.WriteLine(currentTime.ToString("H:mm")); // 0:34
Console.WriteLine(currentTime.ToString("h:mm tt")); // 12:34 AM

This method is the best way of getting time from DateTime if your main goal is to display time in an interface because it gives you the most flexibility in choosing a human-readable format.

FAQ

What are some ways to represent time in .NET?

The System namespace in .NET has three immutable structs to represent time:

  1. DateTime
  2. DateTimeOffset
  3. TImeSpan

What are Ticks in C#?

In C#, tick is the value of 100-nanosecond intervals since 1/1/0001 12:00am.

There are 10,000,000 ticks per second. With so many ticks per second, the underlying data structure needs to be very large. .NET uses UInt64 value type with values ranging from 0 to 18,446,744,073,709,551,615.

Josip Miskovic
About Josip

Josip Miskovic is a software developer at Americaneagle.com. Josip has 10+ years in experience in developing web applications, mobile apps, and games.

Read more posts →
Published on:
Download Free Software Developer Career Guide

I've used these principles to increase my earnings by 63% in two years. So can you.

Dive into my 7 actionable steps to elevate your career.