home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Console Thyself / CsDateClass / CsDateClass.cs next >
Encoding:
Text File  |  2001-01-15  |  988 b   |  37 lines

  1. //------------------------------------------
  2. // CsDateClass.cs ⌐ 2001 by Charles Petzold
  3. //------------------------------------------
  4. using System;
  5.  
  6. class CsDateClass
  7. {
  8.      public static void Main()
  9.      {
  10.           Date mydate = new Date();
  11.  
  12.           mydate.month = 8;
  13.           mydate.day   = 29;
  14.           mydate.year  = 2001;
  15.  
  16.           Console.WriteLine("Day of year = {0}", mydate.DayOfYear());
  17.      }         
  18. }
  19. class Date
  20. {
  21.      public int year;
  22.      public int month;
  23.      public int day;
  24.  
  25.      public static bool IsLeapYear(int year)
  26.      {
  27.           return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0));
  28.      }
  29.      public int DayOfYear()
  30.      {
  31.           int[] MonthDays = new int[] {   0,  31,  59,  90, 120, 151,
  32.                                         181, 212, 243, 273, 304, 334 };
  33.           
  34.           return MonthDays[month - 1] + day + 
  35.                               (month > 2 && IsLeapYear(year) ? 1 : 0);
  36.      }
  37. }