Date vs DateTime

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

I created a simple Date struct for times when you need a simple date without worrying about time portion, timezones, local vs. utc, etc.

Date today = Date.Today;
Date yesterday = Date.Today.AddDays(-1);
Date independenceDay = Date.Parse("2013-07-04");

independenceDay.ToLongString();    // "Thursday, July 4, 2013"
independenceDay.ToShortString();   // "7/4/2013"
independenceDay.ToString();        // "7/4/2013"
independenceDay.ToString("s");     // "2013-07-04"
int july = independenceDay.Month;  // 7

https://github.com/claycephus/csharp-date

Method 3

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.

Method 4

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.

Method 5

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.

Method 6

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.

Method 7

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);
}

Method 8

There is no Date DataType.

However you can use DateTime.Date to get just the Date.

E.G.

DateTime date = DateTime.Now.Date;

Method 9

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.

Method 10

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.

Method 11

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.

Method 12

You could try one of the following:

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

But there is no “Date” type in the BCL.

Method 13

public class AsOfdates
{
    public string DisplayDate { get; set; }
    private DateTime TheDate;
    public DateTime DateValue 
    {
        get 
        { 
            return TheDate.Date; 
        } 

        set 
        { 
            TheDate = value; 
        } 
    }    
}

Method 14

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/


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x