I am trying to create a function similar to Excel's EOMONTH
function in C#. I have written the following, however, I am not entirely sure if the it achieves the equivalent functionality. Is my equivalent of Excel's "Eomonth" function correct and are my tests sufficient?
public static DateTime EOMonth(this DateTime dateTime, int months = 0)
{
DateTime firstDayOfTheMonth = new DateTime(dateTime.Year, dateTime.Month, 1);
return firstDayOfTheMonth.AddMonths(1 + months).AddDays(-1);
}
Tests
[Test]
public void EOMonth_For5jan2011WithNoAddedMonths_ReturnsLastDayOfJan()
{
var expectedDate = DateTime.Parse("31-Jan-2011");
var currentDate = DateTime.Parse("5-Jan-2011");
var result = currentDate.EOMonth();
Assert.That(result, Is.EqualTo(expectedDate));
}
[Test]
public void EOMonth_For5jan2011With_1_AddedMonths_ReturnsLastDayOfFeb2011()
{
var expectedDate = DateTime.Parse("28-Feb-2011");
var currentDate = DateTime.Parse("5-Jan-2011");
var result = currentDate.EOMonth(1);
Assert.That(result, Is.EqualTo(expectedDate));
}
[Test]
public void EOMonth_For5jan2011WithNegative_1_AddedMonths_ReturnsLastDayOfFeb()
{
var expectedDate = DateTime.Parse("31-Dec-2010");
var currentDate = DateTime.Parse("5-Jan-2011");
var result = currentDate.EOMonth(-1);
Assert.That(result, Is.EqualTo(expectedDate));
}
[Test]
public void EOMonth_For28Feb2007_12_AddedMonths_ReturnsLastDayOfFeb2008()
{
var expectedDate = DateTime.Parse("29-Feb-2008");
var currentDate = DateTime.Parse("28-Feb-2007");
var result = currentDate.EOMonth(12);
Assert.That(result, Is.EqualTo(expectedDate));
}