Date vs DateTime

后端 未结 12 1689
暖寄归人
暖寄归人 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:07

    Create a wrapper class. Something like this:

    public class Date:IEquatable,IEquatable
        {
            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.

提交回复
热议问题