Skip to content

Instantly share code, notes, and snippets.

@hatelove
Last active December 15, 2015 15:59
Show Gist options
  • Save hatelove/5285685 to your computer and use it in GitHub Desktop.
Save hatelove/5285685 to your computer and use it in GitHub Desktop.
public class SpecialDate
{
public DateTime GetThreeMonthLater(DateTime original)
{
//msdn reference: http://msdn.microsoft.com/zh-tw/library/system.datetime.addmonths.aspx
//AddMonths 方法會將閏年和月份天數列入考量以計算結果的月份和日期,然後調整產生之 DateTime 物件的日期部分。
//如果產生的日期在產生的月份中不是有效的日期,則會使用產生之月份的最後一個有效日期。
//例如 3 月 31 日 + 1 個月 = 4 月 30 日。產生之 DateTime 物件的當天時間部分仍然與這個執行個體相同。
var result = original.AddMonths(3);
return result;
}
}
[TestClass]
public class UnitTest1
{
[TestMethod]
public void Test2013_03_31()
{
var target = new SpecialDateLibrary.SpecialDate();
var result = target.GetThreeMonthLater(new DateTime(2013, 3, 31));
var expected = new DateTime(2013, 6, 30);
Assert.AreEqual(expected, result);
}
[TestMethod]
public void Test2013_04_01()
{
var target = new SpecialDateLibrary.SpecialDate();
var result = target.GetThreeMonthLater(new DateTime(2013,4,1));
var expected = new DateTime(2013,7,1);
Assert.AreEqual(expected, result);
}
[TestMethod]
public void Test2013_11_29()
{
var target = new SpecialDateLibrary.SpecialDate();
var result = target.GetThreeMonthLater(new DateTime(2013, 11, 29));
var expected = new DateTime(2014, 2, 28);
Assert.AreEqual(expected, result);
}
[TestMethod]
public void Test2013_11_30()
{
var target = new SpecialDateLibrary.SpecialDate();
var result = target.GetThreeMonthLater(new DateTime(2013, 11, 30));
var expected = new DateTime(2014, 2, 28);
Assert.AreEqual(expected, result);
}
[TestMethod]
public void Test2015_11_29()
{
var target = new SpecialDateLibrary.SpecialDate();
var result = target.GetThreeMonthLater(new DateTime(2015, 11, 29));
var expected = new DateTime(2016, 2, 29);
Assert.AreEqual(expected, result);
}
[TestMethod]
public void Test2015_11_30()
{
var target = new SpecialDateLibrary.SpecialDate();
var result = target.GetThreeMonthLater(new DateTime(2015, 11, 30));
var expected = new DateTime(2016, 2, 29);
Assert.AreEqual(expected, result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment