public void IraqWarTest() { DateTime warBegan = new DateTime(2003, 3, 20); DateTime warEnded = new DateTime(2010, 8, 31); DateSpan warLength = new DateSpan(warBegan, warEnded); Console.WriteLine("The Iraq War lasted {0}.", warLength.ToString()); //The Iraq War lasted 7 years, 5 months, 11 days. TestDateSpan(warBegan, warEnded, years: 7, months: 5, days: 11); }
private static void TestDateSpan( DateTime startDate, DateTime endDate, int years = 0, int months = 0, int days = 0) { DateSpan diff = new DateSpan(startDate, endDate); Console.WriteLine( "{0} - {1} : {2}", startDate.ToShortDateString(), endDate.ToShortDateString(), diff.ToString()); Assert.IsTrue( diff.Years == years && diff.Months == months && diff.Days == days, String.Format("FAILURE: expected {0} years, {1} months, {2} days", years, months, days)); }