[C#] Date类型


很多人提出C#需要有个单独的Date类型,以便干净的处理日期。

微软的人声称Datetime类型是Date类型的超集,所以没有必要单独的Date类型。但很多人明显并不认可这个答案。

下面是我提供的一个Date class实现,给需要的同学:

using System;
using System.Globalization;
using System.Runtime.Serialization;

namespace System
{
	/// 
	/// 专门用来处理日期的class,不会有‘时分秒’的干扰
	/// 
	public struct Date : IComparable, IFormattable, ISerializable, IComparable, IEquatable
	{
		private DateTime _dt;	//这个变量的时分秒都应该始终是0

		public static readonly Date MaxValue = new Date(DateTime.MaxValue);
		public static readonly Date MinValue = new Date(DateTime.MinValue);

		public Date(int year, int month, int day)
		{
			this._dt = new DateTime(year, month, day);
		}

		public Date(DateTime dateTime)
		{
			this._dt = dateTime.AddTicks(-dateTime.Ticks % TimeSpan.TicksPerDay);
		}

		public static TimeSpan operator -(Date d1, Date d2)
		{
			return d1._dt - d2._dt;
		}

		public static Date operator -(Date d, TimeSpan t)
		{
			return new Date(d._dt - t);
		}

		public static bool operator !=(Date d1, Date d2)
		{
			return d1._dt != d2._dt;
		}

		public static Date operator +(Date d, TimeSpan t)
		{
			return new Date(d._dt + t);
		}

		public static bool operator <(Date d1, Date d2)
		{
			return d1._dt < d2._dt;
		}

		public static bool operator <=(Date d1, Date d2)
		{
			return d1._dt <= d2._dt;
		}

		public static bool operator ==(Date d1, Date d2)
		{
			return d1._dt == d2._dt;
		}

		public static bool operator >(Date d1, Date d2)
		{
			return d1._dt > d2._dt;
		}

		public static bool operator >=(Date d1, Date d2)
		{
			return d1._dt >= d2._dt;
		}

		public static implicit operator DateTime(Date d)
		{
			return d._dt;
		}

		public static explicit operator Date(DateTime d)
		{
			return new Date(d);
		}

		public int Day
		{
			get
			{
				return this._dt.Day;
			}
		}

		public DayOfWeek DayOfWeek
		{
			get
			{
				return this._dt.DayOfWeek;
			}
		}

		public int DayOfYear
		{
			get
			{
				return this._dt.DayOfYear;
			}
		}

		public int Month
		{
			get
			{
				return this._dt.Month;
			}
		}

		public static Date Today
		{
			get
			{
				return new Date(DateTime.Today);
			}
		}

		public int Year
		{
			get
			{
				return this._dt.Year;
			}
		}

		/// 
		/// 返回上一天
		/// 
		public Date Yesterday
		{
			get
			{
				return this.AddDays(-1);
			}
		}

		/// 
		/// 返回下一天
		/// 
		public Date Tomorrow
		{
			get
			{
				return this.AddDays(1);
			}
		}

		public Date AddDays(int value)
		{
			return new Date(this._dt.AddDays(value));
		}

		public Date AddMonths(int value)
		{
			return new Date(this._dt.AddMonths(value));
		}

		public Date AddYears(int value)
		{
			return new Date(this._dt.AddYears(value));
		}

		public static int Compare(Date d1, Date d2)
		{
			return d1.CompareTo(d2);
		}

		public int CompareTo(Date value)
		{
			return this._dt.CompareTo(value._dt);
		}

		public int CompareTo(object value)
		{
			return this._dt.CompareTo(value);
		}

		public static int DaysInMonth(int year, int month)
		{
			return DateTime.DaysInMonth(year, month);
		}

		public bool Equals(Date value)
		{
			return this._dt.Equals(value._dt);
		}

		public override bool Equals(object value)
		{
			return value is Date && this._dt.Equals(((Date)value)._dt);
		}

		public override int GetHashCode()
		{
			return this._dt.GetHashCode();
		}

		public static bool Equals(Date d1, Date d2)
		{
			return d1._dt.Equals(d2._dt);
		}

		void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
		{
			info.AddValue("ticks", this._dt.Ticks);
		}

		public static bool IsLeapYear(int year)
		{
			return DateTime.IsLeapYear(year);
		}

		public static Date Parse(string s)
		{
			return new Date(DateTime.Parse(s));
		}

		public static Date Parse(string s, IFormatProvider provider)
		{
			return new Date(DateTime.Parse(s, provider));
		}

		public static Date Parse(string s, IFormatProvider provider, DateTimeStyles style)
		{
			return new Date(DateTime.Parse(s, provider, style));
		}

		public static Date ParseExact(string s, string format, IFormatProvider provider)
		{
			return new Date(DateTime.ParseExact(s, format, provider));
		}

		public static Date ParseExact(string s, string format, IFormatProvider provider, DateTimeStyles style)
		{
			return new Date(DateTime.ParseExact(s, format, provider, style));
		}

		public static Date ParseExact(string s, string[] formats, IFormatProvider provider, DateTimeStyles style)
		{
			return new Date(DateTime.ParseExact(s, formats, provider, style));
		}

		public TimeSpan Subtract(Date value)
		{
			return this - value;
		}

		public Date Subtract(TimeSpan value)
		{
			return this - value;
		}

		/// 
		/// "Thursday, July 4, 2013"
		/// 
		/// 
		public string ToLongString()
		{
			return this._dt.ToLongDateString();
		}

		/// 
		/// "7/4/2013"
		/// 
		/// 
		public string ToShortString()
		{
			return this._dt.ToShortDateString();
		}

		/// 
		/// "7/4/2013"
		/// 
		/// 
		public override string ToString()
		{
			return this.ToShortString();
		}

		public string ToString(IFormatProvider provider)
		{
			return this._dt.ToString(provider);
		}

		/// 
		/// format="s"可以返回"2013-07-04"格式
		/// 
		/// 
		/// 
		public string ToString(string format)
		{
			if (format == "O" || format == "o" || format == "s")
			{
				return this.ToString("yyyy-MM-dd");
			}

			return this._dt.ToString(format);
		}

		public string ToString(string format, IFormatProvider provider)
		{
			return this._dt.ToString(format, provider);
		}

		/// 
		/// 获取形如20150702这样格式的日期
		/// 
		/// 
		public int ToInt()
		{
			int ret = _dt.Year * 10000 + _dt.Month * 100 + _dt.Day;
			return ret;
		}

		/// 
		/// 获取形如20150702这样格式的日期
		/// 
		/// 
		public string ToIntString()
		{
			return ToInt().ToString();
		}

		public static bool TryParse(string s, out Date result)
		{
			DateTime d;
			bool success = DateTime.TryParse(s, out d);
			result = new Date(d);
			return success;
		}

		public static bool TryParse(string s, IFormatProvider provider, DateTimeStyles style, out Date result)
		{
			DateTime d;
			bool success = DateTime.TryParse(s, provider, style, out d);
			result = new Date(d);
			return success;
		}

		public static bool TryParseExact(string s, string format, IFormatProvider provider, DateTimeStyles style, out Date result)
		{
			DateTime d;
			bool success = DateTime.TryParseExact(s, format, provider, style, out d);
			result = new Date(d);
			return success;
		}

		public static bool TryParseExact(string s, string[] formats, IFormatProvider provider, DateTimeStyles style, out Date result)
		{
			DateTime d;
			bool success = DateTime.TryParseExact(s, formats, provider, style, out d);
			result = new Date(d);
			return success;
		}

	}

	public static class DateTimeExtensions
	{
		public static Date ToDate(this DateTime dt)
		{
			return new Date(dt);
		}
	}


}

同样的代码也在GitHub上有: https://github.com/wsq003/DateClass


你可能感兴趣的:(C#)