Приветствую Вас, Гость! Регистрация RSS

<Uzh.Soft/>

Воскресенье, 19.05.2024
Главная » 2012 » Декабрь » 11 » Как узнать полный возвраст человека?
23:00
Как узнать полный возвраст человека?
  1. using System;
  2.  
  3. namespace testDate
  4. {
  5.     class Program
  6.     {
  7.         public static void Main(string[] args)
  8.         {
  9.             DateTime compareTo = DateTime.Parse("10.12.1912");
  10.             DateTime now = DateTime.Now;
  11.             var dateSpan = DateTimeSpan.CompareDates(compareTo, now);
  12.             Console.WriteLine("Years: " + dateSpan.Years);
  13.             Console.WriteLine("Months: " + dateSpan.Months);
  14.             Console.WriteLine("Days: " + dateSpan.Days);
  15.             //Console.WriteLine("Hours: " + dateSpan.Hours);
  16.             //Console.WriteLine("Minutes: " + dateSpan.Minutes);
  17.             //Console.WriteLine("Seconds: " + dateSpan.Seconds);
  18.             //Console.WriteLine("Milliseconds: " + dateSpan.Milliseconds);
  19.             Console.Write("Press any key to continue . . .");
  20.             Console.ReadKey(true);
  21.         }
  22.  
  23.         public struct DateTimeSpan 
  24.         {
  25.             private readonly int years;
  26.             private readonly int months;
  27.             private readonly int days;
  28.             private readonly int hours;
  29.             private readonly int minutes;
  30.             private readonly int seconds;
  31.             private readonly int milliseconds;
  32.  
  33.             public DateTimeSpan(int years, int months, int days, int hours, int minutes, int seconds, int milliseconds)
  34.             {
  35.                 this.years = years;
  36.                 this.months = months;
  37.                 this.days = days;
  38.                 this.hours = hours;
  39.                 this.minutes = minutes;
  40.                 this.seconds = seconds;
  41.                 this.milliseconds = milliseconds;
  42.             }
  43.  
  44.             public int Years { get { return years; } }
  45.             public int Months { get { return months; } }
  46.             public int Days { get { return days; } }
  47.             public int Hours { get { return hours; } }
  48.             public int Minutes { get { return minutes; } }
  49.             public int Seconds { get { return seconds; } }
  50.             public int Milliseconds { get { return milliseconds; } }
  51.  
  52.             enum Phase { Years, Months, Days, Done }
  53.  
  54.             public static DateTimeSpan CompareDates(DateTime date1, DateTime date2) 
  55.             {
  56.                 if (date2 < date1)
  57.                 {
  58.                     var sub = date1;
  59.                     date1 = date2;
  60.                     date2 = sub;
  61.                 }
  62.  
  63.                 DateTime current = date1;
  64.                 int years = 0;
  65.                 int months = 0;
  66.                 int days = 0;
  67.  
  68.                 Phase phase = Phase.Years;
  69.                 DateTimeSpan span = new DateTimeSpan();
  70.  
  71.                 while (phase != Phase.Done) 
  72.                 {
  73.                     switch (phase) 
  74.                     {
  75.                         case Phase.Years:
  76.                             if (current.AddYears(years + 1) > date2)
  77.                             {
  78.                                 phase = Phase.Months;
  79.                                 current = current.AddYears(years);
  80.                             }
  81.                             else 
  82.                             {
  83.                                 years++;
  84.                             }
  85.                             break;
  86.                         case Phase.Months:
  87.                             if (current.AddMonths(months + 1) > date2) 
  88.                             {
  89.                                 phase = Phase.Days;
  90.                                 current = current.AddMonths(months);
  91.                             }
  92.                             else 
  93.                             {
  94.                                 months++;
  95.                             }
  96.                             break;
  97.                         case Phase.Days:
  98.                             if (current.AddDays(days + 1) > date2) 
  99.                             {
  100.                                 current = current.AddDays(days);
  101.                                 var timespan = date2 - current;
  102.                                 span = new DateTimeSpan(years, months, days, timespan.Hours, timespan.Minutes, timespan.Seconds, timespan.Milliseconds);
  103.                                 phase = Phase.Done;
  104.                             }
  105.                             else 
  106.                             {
  107.                                 days++;
  108.                             }
  109.                             break;
  110.                     }
  111.                 }
  112.  
  113.                 return span;
  114.             }   
  115.         }
  116.     }
  117. }
Категория: Простые примеры | Просмотров: 608 | Добавил: is | Теги: возвраст, разница двух дат, Difference between two dates | Рейтинг: 0.0/0
Всего комментариев: 0
Имя *:
Email *:
Код *: