DateTime formatting extension method
Notes: I also updated the pieces of code at http://www.extensionmethod.net/Details.aspx?ID=393
In current project, I get some troubles in DateTime class, about format the date time object with the pattern specific string and the current culture, so I decide to code some extension methods in DataTime class in .NET library. And I think it’s it very useful if somebody can use it as right way. I take some idea from this link and coding the enum class for its. After that I also used the Lambda Expression for pass the parameter into the DateTime formatting method. It’s really easy for read code. And below is my implementation for that:
+ DateTimeFormat enum class:
public enum DateTimeFormat
{
[StringValue("unknown")]
UnknownSpecific = 0,
[StringValue("d")]
ShortDate = 1,
[StringValue("D")]
LongDate = 2,
[StringValue("t")]
ShortTime = 3,
[StringValue("T")]
LongTime = 4,
[StringValue("f")]
FullShortDateTime = 5,
[StringValue("F")]
FullLongDateTime = 6,
[StringValue("g")]
GeneralShortDateTime = 7,
[StringValue("G")]
GeneralLongDateTime = 8,
[StringValue("m")]
MonthDayLowerCase = 9,
[StringValue("M")]
MonthDayUpperCase = 10,
[StringValue("r")]
Rfc1123LowerCase = 11,
[StringValue("R")]
Rfc1123UpperCase = 12,
[StringValue("s")]
SortableDateTimeIso8601 = 13,
[StringValue("U")]
UniversalSortableDateTime = 14,
[StringValue("y")]
YearMonthLowerCase = 15,
[StringValue("Y")]
YearMonthUpperCase = 16
}
+ DateTime extension class:
public static class DateTimeExtension
{
/// <summary>
/// Toes the string format.
/// </summary>
/// <param name="source">The source.</param>
/// <param name="dateTimeFormat">The date time format.</param>
/// <returns></returns>
public static string ToStringFormat(this DateTime source, Expression<Func<DateTimeFormat>> dateTimeFormat)
{
var dateTimeFormatCompiled = dateTimeFormat.Compile().Invoke();
var dateTimeStringFormat = StringEnum.GetStringValue(dateTimeFormatCompiled);
var currentCulture = Thread.CurrentThread.CurrentCulture;
return source.ToString(dateTimeStringFormat, currentCulture);
}
}
+ Use it:
var shortDateResult = DateTime.Now.ToStringFormat(() => DateTimeFormat.ShortDate);
var fullLongDateTimeResult = DateTime.Now.ToStringFormat(() => DateTimeFormat.FullLongDateTime);
var rfc1123Result = DateTime.Now.ToStringFormat(() => DateTimeFormat.Rfc1123LowerCase);
+ Some unit testing:
[TestClass]
public class DateTimeFormatTesting
{
[TestMethod]
public void Can_Get_String_Of_Date_Time_Format()
{
var shortDate = StringEnum.GetStringValue(DateTimeFormat.ShortDate);
Assert.AreEqual(shortDate, "d");
var longDate = StringEnum.GetStringValue(DateTimeFormat.LongDate);
Assert.AreEqual(longDate, "D");
var shortTime = StringEnum.GetStringValue(DateTimeFormat.ShortTime);
Assert.AreEqual(shortTime, "t");
var longTime = StringEnum.GetStringValue(DateTimeFormat.LongTime);
Assert.AreEqual(longTime, "T");
var monthDayLowerCase = StringEnum.GetStringValue(DateTimeFormat.MonthDayLowerCase);
Assert.AreEqual(monthDayLowerCase, "m");
var monthDayUpperCase = StringEnum.GetStringValue(DateTimeFormat.MonthDayUpperCase);
Assert.AreEqual(monthDayUpperCase, "M");
var rfc1123UpperCase = StringEnum.GetStringValue(DateTimeFormat.Rfc1123UpperCase);
Assert.AreEqual(rfc1123UpperCase, "R");
// and more testing for this enum at here
// ...
}
}
[TestClass]
public class DateTimeStringFormatTesting
{
private DateTime _originalDateTime;
[TestInitialize]
public void InitTestCase()
{
_originalDateTime = new DateTime(1984, 3, 11, 2, 30, 58);
}
[TestMethod]
public void Can_Get_Short_Date_String()
{
var result = _originalDateTime.ToStringFormat(() => DateTimeFormat.ShortDate);
Assert.AreEqual(_originalDateTime.ToShortDateString(), result);
}
[TestMethod]
public void Can_Get_Long_Date_String()
{
var result = _originalDateTime.ToStringFormat(() => DateTimeFormat.LongDate);
Assert.AreEqual("11 March 1984", result);
}
[TestMethod]
public void Can_Get_Short_Time_String()
{
var result = _originalDateTime.ToStringFormat(() => DateTimeFormat.ShortTime);
Assert.AreEqual("02:30", result);
}
[TestMethod]
public void Can_Get_Long_Time_String()
{
var result = _originalDateTime.ToStringFormat(() => DateTimeFormat.LongTime);
Assert.AreEqual("02:30:58", result);
}
[TestMethod]
public void Can_Get_Full_Short_Date_Time_String()
{
var result = _originalDateTime.ToStringFormat(() => DateTimeFormat.FullShortDateTime);
Assert.AreEqual("11 March 1984 02:30", result);
}
[TestMethod]
public void Can_Get_Full_Long_Date_Time_String()
{
var result = _originalDateTime.ToStringFormat(() => DateTimeFormat.FullLongDateTime);
Assert.AreEqual("11 March 1984 02:30:58", result);
}
[TestMethod]
public void Can_Get_General_Short_Date_Time_String()
{
var result = _originalDateTime.ToStringFormat(() => DateTimeFormat.GeneralShortDateTime);
Assert.AreEqual("11/03/1984 02:30", result);
}
[TestMethod]
public void Can_Get_General_Long_Date_Time_String()
{
var result = _originalDateTime.ToStringFormat(() => DateTimeFormat.GeneralLongDateTime);
Assert.AreEqual("11/03/1984 02:30:58", result);
}
[TestMethod]
public void Can_Get_Month_Day_Lower_Case_String()
{
var result = _originalDateTime.ToStringFormat(() => DateTimeFormat.MonthDayLowerCase);
Assert.AreEqual("11 March", result);
}
[TestMethod]
public void Can_Get_Month_Day_Upper_Case_String()
{
var result = _originalDateTime.ToStringFormat(() => DateTimeFormat.MonthDayUpperCase);
Assert.AreEqual("11 March", result);
}
[TestMethod]
public void Can_Get_Rfc1123_Lower_Case_String()
{
var result = _originalDateTime.ToStringFormat(() => DateTimeFormat.Rfc1123LowerCase);
Assert.AreEqual("Sun, 11 Mar 1984 02:30:58 GMT", result);
}
[TestMethod]
public void Can_Get_Rfc1123_Upper_Case_String()
{
var result = _originalDateTime.ToStringFormat(() => DateTimeFormat.Rfc1123UpperCase);
Assert.AreEqual("Sun, 11 Mar 1984 02:30:58 GMT", result);
}
[TestMethod]
public void Can_Get_Sortable_DateTime_Iso8601_String()
{
var result = _originalDateTime.ToStringFormat(() => DateTimeFormat.SortableDateTimeIso8601);
Assert.AreEqual("1984-03-11T02:30:58", result);
}
[TestMethod]
public void Can_Get_Universal_Sortable_DateTime_String()
{
var result = _originalDateTime.ToStringFormat(() => DateTimeFormat.UniversalSortableDateTime);
Assert.AreEqual("11 March 1984 02:30:58", result);
}
[TestMethod]
public void Can_Get_Year_Month_Lower_Case_String()
{
var result = _originalDateTime.ToStringFormat(() => DateTimeFormat.YearMonthLowerCase);
Assert.AreEqual("March 1984", result);
}
[TestMethod]
public void Can_Get_Year_Month_Upper_Case_String()
{
var result = _originalDateTime.ToStringFormat(() => DateTimeFormat.YearMonthUpperCase);
Assert.AreEqual("March 1984", result);
}
[TestCleanup]
public void TeardownTestCase()
{
if (_originalDateTime != null)
{
GC.SuppressFinalize(_originalDateTime);
}
}
}
That is it. Just 2 classes for this extension and if you have some concern about that, don't hesitate to contact with me. Code for this extension is attached in this post as below. Happy coding, mate!