tags:

views:

1232

answers:

9
+5  Q: 

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". There has to be a datatype that returns just the date... is there?

A: 

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.

Mikko Rantanen
+4  A: 

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.

Jonathan
+17  A: 

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.

Ronald Wildenberg
A: 

You could try one of the following:

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

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

Andrew Hare
A: 

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.

Stevo3000
+1  A: 

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.

C. Ross
A: 

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.

Sam Meldrum
A: 

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);
}
hmcclungiii
+2  A: 

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.

Øyvind Skaar
implicit and explicit are wrong?
Behrooz