public IActionResult Index(PersonAgeModel personAgeModel) { if (ModelState.IsValid) { personAgeModel.Name = personAgeModel.Name.ToUpper(); if (personAgeModel.Birthday != null) { TimeSpan ageToday = personAgeModel.AgeToday().Value; int years = (ageToday.Days / 365); int days = ageToday.Days - (years * 365); ViewBag.Result = personAgeModel.Name + " is " + string.Format("{0} years and {1} days as of {2}", years, days, DateTime.Today.ToString("MM-dd-yyyy")); } else { ViewBag.Result = personAgeModel.Name + " is " + personAgeModel.AgeThisYear().ToString(); } } else { ViewBag.Result = ""; } return(View(personAgeModel)); }
public void AgeByEndOfYearTest() { DateTime Birthday = new DateTime(2000, 1, 1); int ageThisYear = 2020 - Birthday.Year; PersonAgeModel person = new PersonAgeModel() { Birthday = Birthday }; Assert.Equal(ageThisYear, person.AgeThisYear()); }