Date vs DateTime

后端 未结 12 1660
暖寄归人
暖寄归人 2020-11-30 05:13

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

相关标签:
12条回答
  • 2020-11-30 06:01

    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.

    0 讨论(0)
  • 2020-11-30 06:01

    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.

    0 讨论(0)
  • 2020-11-30 06:03

    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.

    0 讨论(0)
  • 2020-11-30 06:05

    You could try one of the following:

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

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

    0 讨论(0)
  • 2020-11-30 06:07

    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.

    0 讨论(0)
  • 2020-11-30 06:07
    public class AsOfdates
    {
        public string DisplayDate { get; set; }
        private DateTime TheDate;
        public DateTime DateValue 
        {
            get 
            { 
                return TheDate.Date; 
            } 
    
            set 
            { 
                TheDate = value; 
            } 
        }    
    }
    
    0 讨论(0)
提交回复
热议问题