Date vs DateTime

Viewed 249527

I am working on a program that requires the date of an event to get returned.

I am looking for a Date, not a DateTime.

Is there a datatype that returns just the date?

14 Answers

No there isn't. DateTime represents some point in time that is composed of a date and a time. However, you can retrieve the date part via the Date property (which is another DateTime with the time set to 00:00:00).

And you can retrieve individual date properties via Day, Month and Year.

UPDATE: In .NET 6 the types DateOnly and TimeOnly are introduced that represent just a date or just a time.

Unfortunately, not in the .Net BCL. Dates are usually represented as a DateTime object with the time set to midnight.

As you can guess, this means that you have all the attendant timezone issues around it, even though for a Date object you'd want absolutely no timezone handling.

Create a wrapper class. Something like this:

public class Date:IEquatable<Date>,IEquatable<DateTime>
    {
        public Date(DateTime date)
        {
            value = date.Date;
        }

        public bool Equals(Date other)
        {
            return other != null && value.Equals(other.value);
        }

        public bool Equals(DateTime other)
        {
            return value.Equals(other);
        }

        public override string ToString()
        {
            return value.ToString();
        }
        public static implicit operator DateTime(Date date)
        {
            return date.value;
        }
        public static explicit operator Date(DateTime dateTime)
        {
            return new Date(dateTime);
        }

        private DateTime value;
    }

And expose whatever of value you want.

The Date type is just an alias of the DateTime type used by VB.NET (like int becomes Integer). Both of these types have a Date property that returns you the object with the time part set to 00:00:00.

DateTime has a Date property that you can use to isolate the date part. The ToString method also does a good job of only displaying the Date part when the time part is empty.

The DateTime object has a Property which returns only the date portion of the value.

    public static void Main()
{
    System.DateTime _Now = DateAndTime.Now;
    Console.WriteLine("The Date and Time is " + _Now);
    //will return the date and time
    Console.WriteLine("The Date Only is " + _Now.Date);
    //will return only the date
    Console.Write("Press any key to continue . . . ");
    Console.ReadKey(true);
}

It seems that .NET 6 is finally introducing a date only type. It will be called DateOnly and there will also be a TimeOnly type added to the BCL in the System namespace.

It is already available in preview 4. Read this blog article for further details.

You can return DateTime where the time portion is 00:00:00 and just ignore it. The dates are handled as timestamp integers so it makes sense to combine the date with the time as that is present in the integer anyway.

For this, you need to use the date, but ignore the time value.

Ordinarily a date would be a DateTime with time of 00:00:00

The DateTime type has a .Date property which returns the DateTime with the time value set as above.

You could try one of the following:

DateTime.Now.ToLongDateString();
DateTime.Now.ToShortDateString();

But there is no "Date" type in the BCL.

As pointed by Dejan and Jonas Lomholdt,

.Net 6 has the DateOnly type, it is a structure that is intended to represent only a date like a year, month, and day.

DateOnly d1 = new DateOnly(2022, 5, 16);
Console.WriteLine(d1);            // 5/16/2022
Console.WriteLine(d1.Year);      // 2021
Console.WriteLine(d1.Month);     // 5
Console.WriteLine(d1.Day);       // 16
Console.WriteLine(d1.DayOfWeek); // Monday



// Manipulation
DateOnly d2 = d1.AddMonths(3);  // You can add days, months, or years. Use negative values to subtract. We are adding 3 months
Console.WriteLine(d2);     // "8/16/2022"  notice there is NO time

// You can use the DayNumber property to find out how many days are between two dates
int days = d2.DayNumber - d1.DayNumber;
Console.WriteLine($"There are {days} days between {d1} and {d2}");  //There are 92 days between 5/16/2022 and 8/16/2022

Full credit goes to Matt Johnson-Pint for this article:

https://devblogs.microsoft.com/dotnet/date-time-and-time-zone-enhancements-in-net-6/

Related