From c9616495a3252f78751f236edd392465200f8cc9 Mon Sep 17 00:00:00 2001 From: DreadMaks <92202592+DreadMaks@users.noreply.github.com> Date: Fri, 13 Jan 2023 21:08:37 +0300 Subject: [PATCH] Create Calculating a person's age.cs --- Calculating a person's age.cs | 37 +++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Calculating a person's age.cs diff --git a/Calculating a person's age.cs b/Calculating a person's age.cs new file mode 100644 index 0000000..55f2153 --- /dev/null +++ b/Calculating a person's age.cs @@ -0,0 +1,37 @@ +using System; + +class Program +{ + static void Main(string[] args) + { + for (int i = 1; i <= 3; i++) + { + DateTime dob = GetDOBFromUser(); + DateTime today = DateTime.Now; + Console.WriteLine("Тестовый пример " + i + ":"); + PrintAge(dob, today); + Console.WriteLine(); + } + } + + static DateTime GetDOBFromUser() + { + Console.WriteLine("Пожалуйста, введите дату вашего рождения в формате гггг-мм-дд : "); + DateTime dob; + while (!DateTime.TryParse(Console.ReadLine(), out dob)) + { + Console.WriteLine("Неверный ввод. Пожалуйста, введите действительную дату рождения в формате yyyy-mm-dd : "); + } + return dob; + } + + static void PrintAge(DateTime dob, DateTime today) + { + TimeSpan ageSpan = today - dob; + int years = (int)(ageSpan.TotalDays / 365.25); + int months = (int)(ageSpan.TotalDays / 30.44); + int days = (int)ageSpan.TotalDays; + + Console.WriteLine("возраст: {0} лет, {1} месяцев, {2} дней", years, months, days); + } +}