In C# 9.0 and later, the DateOnly structure is available in the System namespace. DateOnly is designed to represent a date without the time portion, providing a more efficient and accurate way to work with dates when you don't need to consider time information. It's an improvement over using the DateTime structure, which always includes time information
You can create a DateOnly instance to represent a specific date. It requires a year, month, and day as parameters:
You can access various components of a DateOnly object, such as the year, month, and day:
You can convert a DateOnly instance to a string using the ToString method or a custom format:
You can perform arithmetic operations with DateOnly objects, such as addition and subtraction:
// Add one day;
// Subtract one day
// Add one day;
// Subtract one day
You can compare DateOnly objects using comparison operators (e.g., <,>, <=, >=) to check which one is greater or smaller:
// Compare two DateOnly instances
// Compare two DateOnly instances
// Add one day;
// Subtract one day
// Compare two DateOnly instances
// Add one day;
// Subtract one day
// Compare two DateOnly instances
DateOnly is useful when you need to work with date values in scenarios where time information is not relevant. It provides a more efficient and accurate representation of dates without the overhead of time components. It's especially valuable for applications that work with calendars, schedules, or any domain where only the date part matters.