An Assignable DateTime Class in C#

Introduction

Occasionally you run into a structure in the .NET library that you really wish was a class so you can inherit all its existing properties, override the ones you don't like, and add new properties and methods. Such a class is the DateTime class. Here I was sitting and writing some monitoring software and I wanted to re-assign a few of the properties of my DateTime class.

Let me give you the scenario.I wanted to create a DateTime with today's date and manipulate the time within the date. Seems simple enough, right? Here is what I found I needed to do:

Listing 1 - Creating a time with today's date using two DateTime's
  1. DateTime dt1 = DateTime.Now;  
  2. DateTime dt2 = new DateTime(dt1.Year, dt1.Month, dt1.Day, 9, 30, 0, 0);
So I needed two date times to complete my mission. I suppose I could have done the following:

Listing 2 - Creating a time with today's date using many DateTime.Now's
  1. DateTime dt = new DateTime(DateTime.Now.Year,DateTime.Now.Month, DateTime.Now.Day, 9, 30, 0, 0);
But I suspect this an expensive call with all the DateTime.Now's in there. What would be ideal would be able to create a single DateTime class and assign it the hours, minutes, etc. that it needs. When I tried to assign values to Hours and Minutes properties, I soon found out that were read only:

Listing 3 - Illegal version of assigning hours and minutes to a DateTime with today's date
  1. DateTimeExtended dt = DateTime.Now;  
  2. dt.Hour = 6;// illegal, read-only property  
  3. dt.Minute = 59; // illegal, read-only property 
In order to accomplish what I really wanted to do, I created a new class called DateTimeExtended. Because DateTime is a structure, the only way to glom the properties of DateTime is to create an aggregate member of the DateTimeExtended class that is a DateTime type. Then you need to propagate the DateTime properties up through the DateTimeExtended class.

DateTime in C#

Figure 1 - UML Design of the DateTimeExtended class reverse engineered using WithClass


All I had to do now was override a few constructors and propagate the properties in which I'm interested. So how do I go about making Hour, Minute, and Second assignable? I want to leverage the DateTime object inside my DateTimeExtended class and encapsulate all the workarounds inside the class so as not to confuse the user. Below is the Hour property of the DateTime class. Note that I took advantage of the AddHours method to accomplish my goal of assigning the Hour property. This exact same technique is used for all the other DateTime properties (Year, Month, Day, Minute, Millisecond, etc.)

Listing 4 - Propogating an assignable Hour property from the DateTime class
  1. public int Hour  
  2. {  
  3.     get  
  4.     {  
  5.         return _val.Hour; // just propogate the   
  6.         // DateTime property here  
  7.         // for the get  
  8.     }  
  9.     set  
  10.     {  
  11.         // the set is a bit more complicated.....  
  12.         // first subtract off existing hours  
  13.         _val = _val.Subtract(new TimeSpan(_val.Hour, 0, 0));  
  14.         // then add back your assigned hours  
  15.         _val = _val.AddHours(value);  
  16.     }  
  17. }
Listing 4 gives you the idea of how I can propagate any existing methods or properties of the DateTime class and use them however I wish. Another example is propagating the powerful and formattable ToString method of the DateTime class shown in listing 5.

Listing 5 - Propagating the ToString method for formatting date time strings
  1. public string ToString(string format)  
  2. {  
  3.     return _val.ToString(format); // just propagate to   
  4.     // the DateTime class  
  5. }
Since DateTimeExtended is simply a subset of DateTime, you can also create a static method that allows you to implicitly assign a DateTimeExtended object to a DateTime object. This way the compiler will automatically allow the conversion without the explicit (DateTime) prefix whenever you reference it to a DateTime type.
 
Listing 6 - Implicit conversion from DateTimeExtended to DateTime
  1. // implicitly conversion from DateTimeExtended to DateTime. returns  
  2. // the internal DateTime  
  3. public static implicit operator DateTime(DateTimeExtended x)  
  4. {  
  5.     return x._val;  
  6. }   
By utilizing a constructor of the DateTimeExtended class we can also do an implicit conversion in the other direction. Another word, we can assign a DateTimeExtended class to a DateTime class:

Listing 7 - Implicit conversion from DateTime to DateTimeExtended
  1. // Implicit Conversion from DateTime to DateTimeExtended  
  2. // creates a new DateTimeExtended and returns it  
  3. public static implicit operator DateTimeExtended(DateTime x)  
  4. {  
  5.     // Construct a date time extended class from a DateTime  
  6.     DateTimeExtended dt = new DateTimeExtended(x);  
  7.     return dt;  
  8. }  
  9. // constructor for constructing a DateTimeExtended  
  10. // class from a DateTime class  
  11. public DateTimeExtended(DateTime dt)  
  12. {  
  13.     _val = dt;  
  14. }  
Implementing the DateTimeExtended Class

Now we can do what we originally set out to do. We can create a DateTimeExtended class with today's date and assign it the time properties we wish. Below is a method demonstrating how to use the DateTimeExtended class to do just that. The method creates a new DateTimeExtended with today's date. It prints the original time and date out to the console. It assigns a new time to the DateTimeExtended object, and it writes out the new time to the console using the special datetime formatting capability.

Listing 8 - Demonstrating the DateTimeExtended class by assigning a new Time through its properties
  1. /// <summary>  
  2. /// Demonstrates using the DateTimeExtended class  
  3. /// </summary>  
  4. public void Show()  
  5. {  
  6.     // Assign the DateTimeExtended class implicitly  
  7.     // with today's date  
  8.     DateTimeExtended assignableDateTime = DateTime.Now;  
  9.     // write the initial time to the console  
  10.     Console.WriteLine("The time is {0}", assignableDateTime.ToString("MMM-dd h:mm"));  
  11.     // assign a new time to hours and minutes  
  12.     assignableDateTime.Hour = 6;  
  13.     assignableDateTime.Minute = 59;  
  14.     // write the new time to the console  
  15.     Console.WriteLine("The time is now {0}",   
  16.     assignableDateTime.ToString("MMM-dd h:mm"));  
  17. }    

Further Readings

Here are some more articles on DateTime you may want to check out.