⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch7_06.cs

📁 《c#技术内幕代码》
💻 CS
字号:
using System;
using System.Globalization;

class CH7_6
{
   public static void Main()
   {
      // Create some date/time objects
      DateTime dt = new DateTime();
      DateTime dt1 = new DateTime( 2001,12,31 );
      DateTime dt2 = new DateTime( 2000,12,31,23,59,59);

      // Print them out as strings      
      Console.WriteLine("DT as string: {0}", dt.ToString() );
      Console.WriteLine("DT1 as string: {0}", dt1.ToString() );
      Console.WriteLine("DT2 as string: {0}", dt2.ToString() );
      
      // Do some comparisons
      if ( dt2 < dt1 )
         Console.WriteLine("Dt2 < Dt1" );
      else
         if ( dt2 == dt1 )
	    Console.WriteLine("Dt2 == Dt1");
	 else
	    Console.WriteLine("Dt2 > Dt1");

      // One year as a time span
      TimeSpan year = new TimeSpan( 365 * TimeSpan.TicksPerDay );
      
      // Add a few of these to the date/time object	    
      for ( int i=0; i<10; ++i )
         dt += year;
      
      Console.WriteLine("DT as string: {0}", dt.ToString() );
      
      // Look at the min and max date/time values
      Console.WriteLine("Min Date: {0}", DateTime.MinValue.ToString () );
      Console.WriteLine("Max Date: {0}", DateTime.MaxValue.ToString () );
      
      // Current date and time
      Console.WriteLine("Current Date and Time: {0}", DateTime.Now.ToString());
      Console.WriteLine("Current Date Only: {0}", DateTime.Today.ToString());
      
      // Do some leap year checks
      int[] years = {1984, 2000, 1999, 2002 };
      for ( int i=0; i<years.Length; ++i )
      {
          if ( DateTime.IsLeapYear(years[i]) )
             Console.WriteLine("Year {0} is a leap year", years[i] );
          else
             Console.WriteLine("Year {0} is NOT a leap year", years[i] );
      }
      
      // Do some date time math
      DateTime today = DateTime.Today;
      today = today + new TimeSpan(TimeSpan.TicksPerDay);
      Console.WriteLine("Tomorrow is: {0}", today.ToString() );
      today = DateTime.Today - new TimeSpan(7*TimeSpan.TicksPerDay);
      Console.WriteLine("Last Week on this day it was: {0}", today.ToString() );
      
      // last thing to do: some pretty formatting
      string[] format = {
          "d", "D",
          "f", "F",
          "g", "G",
          "m",
          "r",
          "s",
          "t", "T",
          "u", "U",
          "y",
          "dddd, MMMM dd yyyy",
          "ddd, MMM d \"'\"yy",
          "dddd, MMMM dd",
          "M/yy",
          "dd-MM-yy",
      };
      string date;
      for (int i = 0; i < format.Length; i++)
      {
        date = dt.Format(format[i], DateTimeFormatInfo.InvariantInfo);
        Console.WriteLine(String.Concat(format[i], " :" , date));
      }
       
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -