Search Forum
(53671 Postings)
Search Site/Articles

Archived Articles
712 Articles

C# Books
C# Consultants
What Is C#?
Download Compiler
Code Archive
Archived Articles
Advertise
Contribute
C# Jobs
Beginners Tutorial
C# Contractors
C# Consulting
Links
C# Manual
Contact Us
Legal

GoDiagram for .NET from Northwoods Software www.nwoods.com


 
Printable Version

Custom ShortDate type (struct: Icomparable)
By Stefan Prodan

Here it is a custom C#.NET Date type (struct) that will help you deal with converting to and from strings. The struct implements the IComparable interface and overrides the ToString, Equals and GetHashCode() functions.

In my work I use short dates all the time so this struct has only 3 members: day, month and year. It is realy easy to extend the struct to full datetime, just drop a message to my blog if you need it. (http://www.alephdev.blogspot.com)

Code:

using System;
using System.Text.RegularExpressions;

[Serializable]
public struct Date : IComparable
{
  #region private members

  private UInt16 _day;
  private UInt16 _month;
  private UInt16 _year;
  #endregion

  #region public members

  public UInt16 Day
  {
    get { return _day; }
    set
    {
      if (value > 31)
      {
        throw new ArgumentException("Day out of range[31]");
      }
      else
      {
        _day = value;
      }
    }
  }

  public UInt16 Month
  {
    get { return _month; }
    set
    {
      if (value > 12)
      {
        throw new ArgumentException("Month out of range[12]");
      }
      else
      {
        _month = value;
      }
    }
  }

  public UInt16 Year
  {
    get { return _year; }
    set
    {
      if (value.ToString().Length > 4)
      {
        throw new ArgumentException("Year out of range.");
      }
      else
      {
        _year = value;
      }
    }
  }

  #endregion

  #region constructor

  public Date(DateTime dt)
  {
    _month = Convert.ToUInt16(dt.Month);
    _day = Convert.ToUInt16(dt.Day);
    _year = Convert.ToUInt16(dt.Year);

  }

  #endregion

  #region functiones

  public bool IsDateTime(string dt)
  {
    Regex rgx = new Regex(@"(?\d{1,2})/(?\d{1,2})/(?(?:\d{4}|\d{2}))");
    if (rgx.IsMatch(dt))
    {
      return true;
    }
    else
    {
      return false;
    }
  }

  public DateTime StringToDate(string dt)
  {
    if (IsDateTime(dt))
    {
      _day = Convert.ToUInt16(dt.Split('/')[0]);
      _month = Convert.ToUInt16(dt.Split('/')[1]);
      _year = Convert.ToUInt16(dt.Split('/')[2]);
    }
    else
    {
      throw new ArgumentException("The string can't be converted to a date");
    }

    return new DateTime(Year, Month, Day);

  }

  public DateTime ToDateTime()
  {
    return new DateTime(Year, Month, Day);
  }

  public static DateTime ToDateTime(Date dt)
  {
    return new DateTime(dt.Year, dt.Month, dt.Day);
  }

  #endregion

  #region IComparable & override implementation

  public int CompareTo(object obj)
  {
    Date dt = (Date) obj;
    UInt16 i = 0;
    if (dt._day == _day)
    {
      i += 100;
    }
    if (dt._month == _month)
    {
      i += 010;
    }
    if (dt._year == _year)
    {
      i += 001;
    }

    return i;
  }

  public override bool Equals(object obj)
  {
    Date dt = (Date) obj;
    if (dt._day == _day && dt._month == _month && dt._year == _year)
    {
      return true;
    }
    else
    {
      return false;
    }
  }

  public override int GetHashCode()
  {
    return (_day ^ _month ^ _year);
  }

  public override string ToString() 
  {
    return (_day.ToString() + "/" + _month.ToString() + "/" + _year.ToString());
  }

  #endregion
}