How to use DateOnly and TimeOnly | VB.Net

In VB.NET, starting from .NET 6, the DateOnly and TimeOnly types have been introduced as part of the System namespace to represent date-only and time-only values respectively. These types are useful when you want to work with date or time components independently without considering time zones. They provide a number of benefits over the traditional DateTime type, including:

  1. Improved performance: DateOnly and TimeOnly are immutable structs, which means that they are more efficient to use than the mutable DateTime type.
  2. Better accuracy: DateOnly and TimeOnly represent date and time values more accurately than DateTime, which can be important for applications that require high precision.
  3. Increased readability: DateOnly and TimeOnly make it easier to write code that is specific to dates and times, which can improve the readability and maintainability of your code.


DateOnly represents a date without a time component or time zone information. It provides methods and properties for working with date values.

Dim dateOnlyValue As DateOnly = DateOnly.FromDateTime(DateTime.Now) Console.WriteLine($"Date: {dateOnlyValue}")


TimeOnly represents a time without a date component or time zone information. It provides methods and properties for working with time values.

Dim timeOnlyValue As TimeOnly = TimeOnly.FromDateTime(DateTime.Now) Console.WriteLine($"Time: {timeOnlyValue}")

Here's an example that demonstrates creating and working with DateOnly and TimeOnly values:

Dim now As DateTime = DateTime.Now Dim dateOnlyValue As DateOnly = DateOnly.FromDateTime(now) Dim timeOnlyValue As TimeOnly = TimeOnly.FromDateTime(now) Console.WriteLine($"Original DateTime: {now}") Console.WriteLine($"DateOnly: {dateOnlyValue}") Console.WriteLine($"TimeOnly: {timeOnlyValue}")

In this example, we first create a DateTime value, and then we convert it into DateOnly and TimeOnly values using the FromDateTime method. This allows us to work with the date and time components separately.

Both DateOnly and TimeOnly are useful when you want to perform operations specifically on date or time values, such as calculations, comparisons, or formatting, without the complexities introduced by time zones and full DateTime objects. These types help improve code clarity and maintainability in scenarios where you need to work with isolated date or time information.


DateOnly and TimeOnly are types introduced in .NET 6 for working with date-only and time-only values respectively. DateOnly represents dates without time components, and TimeOnly represents times without date components or time zone information. These types simplify the handling of date and time components independently, enhancing code clarity and maintainability.