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()); }
public void AgeTodayTest() { DateTime Birthday = new DateTime(2004, 4, 9); DateTime Today = DateTime.Now; TimeSpan AgeToday = Today - Birthday; AgeToday = new TimeSpan(AgeToday.Days * 24, AgeToday.Hours * 60, 0); PersonAgeModel PersonAge = new PersonAgeModel() { Birthday = Birthday }; TimeSpan AgeModelAgeToday = PersonAge.AgeToday().Value; AgeModelAgeToday = new TimeSpan(AgeModelAgeToday.Days * 24, AgeModelAgeToday.Hours * 60, 0); Assert.Equal(AgeToday, AgeModelAgeToday); }
public void CalculateAgeThisYearTest(int BirthdayYear, int BirthdayMonth, int BirthdayDay) { DateTime Birthday = new DateTime(BirthdayYear, BirthdayMonth, BirthdayDay); DateTime Now = DateTime.Now; TimeSpan Age = Now - Birthday; Age = new TimeSpan(Age.Hours, Age.Minutes, 0); PersonAgeModel person = new PersonAgeModel() { Birthday = Birthday }; TimeSpan TestAge = person.AgeToday().Value; TestAge = new TimeSpan(TestAge.Hours, TestAge.Minutes, 0); Assert.Equal(Age, TestAge); }