Csharp/CSharp Tutorial/Date Time/DateTime — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 12:20, 26 мая 2010

Содержание

Add seconds to current time

using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
public class MainClass{
   public static void Main(){
        DateTime endTime = DateTime.Now.AddSeconds(123);
        while (DateTime.Now < endTime)
        {
            Console.WriteLine(DateTime.Now.TimeOfDay.ToString());
        }
   }
}
14:25:59.9687500
14:25:59.9687500
14:25:59.9687500
14:25:59.9687500
14:25:59.9687500
14:25:59.9687500
14:25:59.9687500
^CTerminate batch job (Y/N)? n

Benchmark Timing with DateTime

using System;
using System.Collections.Generic;
using System.Text;
   public class MyMath
   {
      public int DoSum( int n )
      {
         int result = 0;
         for ( int i = 1; i <= n; i++ )
         {
            result += i;
         }
         return result;
      }
      public int DoSum2()
      {
         return 1 + 20;
      }
   }
   public class TestDriver
   {
      public static void Main()
      {
         int val = 20;  
         int iterations = 1000000;
         int result = 0;
         MyMath m = new MyMath();
         DateTime startTime = DateTime.Now;
         for ( int i = 0; i < iterations; i++ )
         {
            result = m.DoSum( val );
         }
         TimeSpan elapsed =DateTime.Now - startTime;
         Console.WriteLine("Loop: Sum of ({0}) = {1}",val, result );
         Console.WriteLine(elapsed.TotalMilliseconds.ToString() );
         startTime = DateTime.Now;
         for ( int i = 0; i < iterations; i++ )
         {
            result = m.DoSum2();
         }
         elapsed = DateTime.Now - startTime;
         Console.WriteLine("Brute Force: Sum of ({0}) = {1}",val, result );
         Console.WriteLine(elapsed.TotalMilliseconds );
      }
   }

Create a DateTime instance, specifying the number of ticks

using System;
class MainClass {
  public static void DisplayDateTime(string name, DateTime myDateTime) {
    Console.WriteLine(name + " = " + myDateTime);
    Console.WriteLine(name + ".Year = " + myDateTime.Year);
    Console.WriteLine(name + ".Month = " + myDateTime.Month);
    Console.WriteLine(name + ".Day = " + myDateTime.Day);
    Console.WriteLine(name + ".Hour = " + myDateTime.Hour);
    Console.WriteLine(name + ".Minute = " + myDateTime.Minute);
    Console.WriteLine(name + ".Second = " + myDateTime.Second);
    Console.WriteLine(name + ".Millisecond = " + myDateTime.Millisecond);
    Console.WriteLine(name + ".Ticks = " + myDateTime.Ticks);
  }
  public static void Main()
  {
    
    DateTime myDateTime4 = new DateTime(0);
    DisplayDateTime("myDateTime4", myDateTime4);
  }
}
myDateTime4 = 01/01/0001 12:00:00 AM
myDateTime4.Year = 1
myDateTime4.Month = 1
myDateTime4.Day = 1
myDateTime4.Hour = 0
myDateTime4.Minute = 0
myDateTime4.Second = 0
myDateTime4.Millisecond = 0
myDateTime4.Ticks = 0

Create a DateTime instance, specifying the year, month, and day

using System;
class MainClass {
  public static void DisplayDateTime(string name, DateTime myDateTime) {
    Console.WriteLine(name + " = " + myDateTime);
    Console.WriteLine(name + ".Year = " + myDateTime.Year);
    Console.WriteLine(name + ".Month = " + myDateTime.Month);
    Console.WriteLine(name + ".Day = " + myDateTime.Day);
    Console.WriteLine(name + ".Hour = " + myDateTime.Hour);
    Console.WriteLine(name + ".Minute = " + myDateTime.Minute);
    Console.WriteLine(name + ".Second = " + myDateTime.Second);
    Console.WriteLine(name + ".Millisecond = " + myDateTime.Millisecond);
    Console.WriteLine(name + ".Ticks = " + myDateTime.Ticks);
  }
  public static void Main()
  {
    
    int year = 2002;
    int month = 12;
    int day = 25;
    DateTime myDateTime = new DateTime(year, month, day);
    DisplayDateTime("myDateTime", myDateTime);
  }
}
myDateTime = 25/12/2002 12:00:00 AM
myDateTime.Year = 2002
myDateTime.Month = 12
myDateTime.Day = 25
myDateTime.Hour = 0
myDateTime.Minute = 0
myDateTime.Second = 0
myDateTime.Millisecond = 0
myDateTime.Ticks = 631763712000000000

Create a DateTime instance, specifying the year, month, day, and JulianCalendar object

using System;
class MainClass {
  public static void DisplayDateTime(string name, DateTime myDateTime) {
    Console.WriteLine(name + " = " + myDateTime);
    Console.WriteLine(name + ".Year = " + myDateTime.Year);
    Console.WriteLine(name + ".Month = " + myDateTime.Month);
    Console.WriteLine(name + ".Day = " + myDateTime.Day);
    Console.WriteLine(name + ".Hour = " + myDateTime.Hour);
    Console.WriteLine(name + ".Minute = " + myDateTime.Minute);
    Console.WriteLine(name + ".Second = " + myDateTime.Second);
    Console.WriteLine(name + ".Millisecond = " + myDateTime.Millisecond);
    Console.WriteLine(name + ".Ticks = " + myDateTime.Ticks);
  }
  public static void Main()
  {
    int year = 2002;
    int month = 12;
    int day = 25;
    int hour = 23;
    int minute = 30;
    int second = 12;
    int millisecond = 5;
    DateTime myDateTime = new DateTime(year, month, day, hour, minute, second, millisecond);
    DisplayDateTime("myDateTime", myDateTime);
    
    System.Globalization.JulianCalendar myCalendar = new System.Globalization.JulianCalendar();
    DateTime myDateTime3 = new DateTime(year, month, day, myCalendar);
    DisplayDateTime("myDateTime3", myDateTime3);
  }
}
myDateTime = 25/12/2002 11:30:12 PM
myDateTime.Year = 2002
myDateTime.Month = 12
myDateTime.Day = 25
myDateTime.Hour = 23
myDateTime.Minute = 30
myDateTime.Second = 12
myDateTime.Millisecond = 5
myDateTime.Ticks = 631764558120050000
myDateTime3 = 07/01/2003 12:00:00 AM
myDateTime3.Year = 2003
myDateTime3.Month = 1
myDateTime3.Day = 7
myDateTime3.Hour = 0
myDateTime3.Minute = 0
myDateTime3.Second = 0
myDateTime3.Millisecond = 0
myDateTime3.Ticks = 631774944000000000

Create a DateTime instance, specifying the year, month, day, hour, minute, second, and millisecond

using System;
class MainClass {
  public static void DisplayDateTime(string name, DateTime myDateTime) {
    Console.WriteLine(name + " = " + myDateTime);
    Console.WriteLine(name + ".Year = " + myDateTime.Year);
    Console.WriteLine(name + ".Month = " + myDateTime.Month);
    Console.WriteLine(name + ".Day = " + myDateTime.Day);
    Console.WriteLine(name + ".Hour = " + myDateTime.Hour);
    Console.WriteLine(name + ".Minute = " + myDateTime.Minute);
    Console.WriteLine(name + ".Second = " + myDateTime.Second);
    Console.WriteLine(name + ".Millisecond = " + myDateTime.Millisecond);
    Console.WriteLine(name + ".Ticks = " + myDateTime.Ticks);
  }
  public static void Main()
  {
    int hour = 23;
    int minute = 30;
    int second = 12;
    int millisecond = 5;
    int year = 2004;
    int month = 5;
    int day = 12;
    DateTime myDateTime = new DateTime(year, month, day, hour, minute, second, millisecond);
    DisplayDateTime("myDateTime", myDateTime);
  }
}
myDateTime = 12/05/2004 11:30:12 PM
myDateTime.Year = 2004
myDateTime.Month = 5
myDateTime.Day = 12
myDateTime.Hour = 23
myDateTime.Minute = 30
myDateTime.Second = 12
myDateTime.Millisecond = 5
myDateTime.Ticks = 632200014120050000

Create a DateTime with the current date and time

using System;
class MainClass
{
    public static void Main(string[] args)
    {
        DateTime now = DateTime.Now;
        Console.WriteLine("Now   : {0}", now);
    }
}
Now   : 25/03/2007 2:10:52 PM

Create a TimeSpan instance, and add it to DateTime

using System;
class MainClass {
  public static void DisplayDateTime(string name, DateTime myDateTime) {
    Console.WriteLine(name + " = " + myDateTime);
    Console.WriteLine(name + ".Year = " + myDateTime.Year);
    Console.WriteLine(name + ".Month = " + myDateTime.Month);
    Console.WriteLine(name + ".Day = " + myDateTime.Day);
    Console.WriteLine(name + ".Hour = " + myDateTime.Hour);
    Console.WriteLine(name + ".Minute = " + myDateTime.Minute);
    Console.WriteLine(name + ".Second = " + myDateTime.Second);
    Console.WriteLine(name + ".Millisecond = " + myDateTime.Millisecond);
    Console.WriteLine(name + ".Ticks = " + myDateTime.Ticks);
  }
  public static void Main()
  {
    
    DateTime myDateTime4 = new DateTime(0);
    TimeSpan myTimeSpan = new TimeSpan(4, 12, 10);
    myDateTime4 += myTimeSpan;
    DisplayDateTime("myDateTime4", myDateTime4);
    
    
  }
}
myDateTime4 = 01/01/0001 4:12:10 AM
myDateTime4.Year = 1
myDateTime4.Month = 1
myDateTime4.Day = 1
myDateTime4.Hour = 4
myDateTime4.Minute = 12
myDateTime4.Second = 10
myDateTime4.Millisecond = 0
myDateTime4.Ticks = 151300000000

Create a TimeSpan representing 1 week and subtract from a DataTime

using System;
class MainClass
{
    public static void Main(string[] args)
    {
        TimeSpan oneWeek = new TimeSpan(4, 12, 0, 0) + new TimeSpan(2, 12, 0, 0);
            
        DateTime now = DateTime.Now;
        // Create a DateTime representing 1 week ago.
        DateTime past = now - oneWeek;
        
        Console.WriteLine("Past  : {0}", past);
    
    }
}
Past  : 18/03/2007 2:10:53 PM

Creating specific date/times: year, month, day, hour, minute, second

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using System.Security.Cryptography;
public class MainClass
{
    public static void Main()
    {
        DateTime dt1 = new DateTime(2004, 10, 19);
        Console.WriteLine("dt1: {0}", dt1);
        DateTime dt2 = new DateTime(2004, 10, 19, 22, 47, 35);
        Console.WriteLine("dt2: {0}", dt2);
        DateTime dt3 = new DateTime(2004, 10, 19, 22, 47, 35, 259);
        Console.WriteLine("dt3: {0}", dt3);
    }
}
dt1: 19/10/2004 12:00:00 AM
dt2: 19/10/2004 10:47:35 PM
dt3: 19/10/2004 10:47:35 PM

Display the Date, Day, DayOfWeek, DayOfYear, Ticks, and TimeOfDayProperties of DateTime

using System;
class MainClass
{
  public static void Main()
  {
    
    DateTime myDateTime = DateTime.Now;
    Console.WriteLine("myDateTime.Date = " + myDateTime.Date);
    Console.WriteLine("myDateTime.Day = " + myDateTime.Day);
    Console.WriteLine("myDateTime.DayOfWeek = " + myDateTime.DayOfWeek);
    Console.WriteLine("myDateTime.DayOfYear = " + myDateTime.DayOfYear);
    Console.WriteLine("myDateTime.Ticks = " + myDateTime.Ticks);
    Console.WriteLine("myDateTime.TimeOfDay = " + myDateTime.TimeOfDay);

  }
}
myDateTime.Date = 25/03/2007 12:00:00 AM
myDateTime.Day = 25
myDateTime.DayOfWeek = Sunday
myDateTime.DayOfYear = 84
myDateTime.Ticks = 633104295802812500
myDateTime.TimeOfDay = 14:26:20.2812500

Use the AddYears(), AddMonths(), AddDays(), AddMinutes(), and AddSeconds() methods to add periods to a DateTime

using System;
class MainClass
{
  public static void Main()
  {
    DateTime myDateTime9 = new DateTime(2004, 1, 1);
    Console.WriteLine("Initial myDateTime9 = " + myDateTime9);
    myDateTime9 = myDateTime9.AddYears(1);
    myDateTime9 = myDateTime9.AddMonths(5);
    myDateTime9 = myDateTime9.AddDays(3);
    myDateTime9 = myDateTime9.AddMinutes(30);
    myDateTime9 = myDateTime9.AddSeconds(15);
    Console.WriteLine("Final myDateTime9 = " + myDateTime9);
  }
}
Initial myDateTime9 = 01/01/2004 12:00:00 AM
Final myDateTime9 = 04/06/2005 12:30:15 AM

Use the Compare() method to compare DateTime instances

using System;
class MainClass
{
  public static void Main()
  {
    DateTime myDateTime3 = new DateTime(2004, 1, 13);
    DateTime myDateTime4 = new DateTime(2004, 1, 14);
    Console.WriteLine("myDateTime3 = " + myDateTime3);
    Console.WriteLine("myDateTime4 = " + myDateTime4);
    int intResult = DateTime.rupare(myDateTime3, myDateTime4);
    Console.WriteLine("DateTime.rupare(myDateTime3, myDateTime4) = " + DateTime.rupare(myDateTime3, myDateTime4));
  }
}
myDateTime3 = 13/01/2004 12:00:00 AM
myDateTime4 = 14/01/2004 12:00:00 AM
DateTime.rupare(myDateTime3, myDateTime4) = -1

Use the DaysInMonth() method to retrieve the number of days in a particular month and year

using System;
class MainClass
{
  public static void Main()
  {
    int days = DateTime.DaysInMonth(2004, 1);
    Console.WriteLine("DateTime.DaysInMonth(2004, 1) = " + days);
  }
}
DateTime.DaysInMonth(2004, 1) = 31

Use the Equals() method to compare DateTime instances

using System;
class MainClass
{
  public static void Main()
  {
    DateTime myDateTime3 = new DateTime(2004, 1, 13);
    DateTime myDateTime4 = new DateTime(2004, 1, 14);
    Console.WriteLine("myDateTime3 = " + myDateTime3);
    Console.WriteLine("myDateTime4 = " + myDateTime4);
    
    
    bool boolResult = DateTime.Equals(myDateTime3, myDateTime4);
    Console.WriteLine("DateTime.Equals(myDateTime3, myDateTime4) = " + boolResult);
    boolResult = myDateTime3.Equals(myDateTime4);
    Console.WriteLine("myDateTime3.Equals(myDateTime4) is " + boolResult);
  }
}
myDateTime3 = 13/01/2004 12:00:00 AM
myDateTime4 = 14/01/2004 12:00:00 AM
DateTime.Equals(myDateTime3, myDateTime4) = False
myDateTime3.Equals(myDateTime4) is False

Use the FromFileTime() method to convertan operating system file timestamp to a DateTime

using System;
class MainClass
{
  public static void Main()
  {
    DateTime myDateTime10 = new DateTime(2004, 1, 15, 23, 2, 5);
    long myFileTime = myDateTime10.ToFileTime();
    DateTime myDateTime11 = DateTime.FromFileTime(myFileTime);
    Console.WriteLine("DateTime.FromFileTime() = " + myDateTime11);
  }
}
DateTime.FromFileTime() = 15/01/2004 11:02:05 PM

Use the IsLeapYear() method to determine if a particular year is a leap year

using System;
class MainClass
{
  public static void Main()
  {
    bool boolResult = DateTime.IsLeapYear(2004);
    Console.WriteLine("DateTime.IsLeapYear(2004) = " + boolResult);
  }
}
DateTime.IsLeapYear(2004) = True

Use the Now and UtcNow properties to get the currrent date and time

using System;
class MainClass
{
  public static void Main()
  {
    
    Console.WriteLine("DateTime.Now = " + DateTime.Now);
    Console.WriteLine("DateTime.UtcNow = " + DateTime.UtcNow);
    DateTime myDateTime = DateTime.Now;
    Console.WriteLine("myDateTime = " + myDateTime);
    DateTime myDateTime2 = DateTime.UtcNow;
    Console.WriteLine("myDateTime = " + myDateTime);
  }
}
DateTime.Now = 25/03/2007 2:26:19 PM
DateTime.UtcNow = 25/03/2007 9:26:19 PM
myDateTime = 25/03/2007 2:26:19 PM
myDateTime = 25/03/2007 2:26:19 PM

Use the overloaded addition operator (+) to add a TimeSpan to a DateTime

using System;
class MainClass
{
  public static void Main()
  {
    
    DateTime myDateTime = DateTime.Parse("1/11/2004 23:10:30");
    
    TimeSpan myTimeSpan = new TimeSpan(1, 2, 4, 10);
    DateTime myDateTime8 = myDateTime + myTimeSpan;
    Console.WriteLine("myDateTime + myTimeSpan = " + myDateTime8);
  }
}
myDateTime + myTimeSpan = 03/11/2004 1:14:40 AM

Use the overloaded less than operator (

using System;
class MainClass
{
  public static void Main()
  {
    DateTime myDateTime3 = new DateTime(2004, 1, 13);
    DateTime myDateTime4 = new DateTime(2004, 1, 14);
    Console.WriteLine("myDateTime3 = " + myDateTime3);
    Console.WriteLine("myDateTime4 = " + myDateTime4);
    
    bool boolResult = myDateTime3 < myDateTime4;
    Console.WriteLine("myDateTime3 < myDateTime4 is " +  boolResult);
  }
}
myDateTime3 = 13/01/2004 12:00:00 AM
myDateTime4 = 14/01/2004 12:00:00 AM
myDateTime3 < myDateTime4 is True

Use the overloaded subtraction operator (-) to subtract a TimeSpan from a DateTime

using System;
class MainClass
{
  public static void Main()
  {
    
    DateTime myDateTime = DateTime.Parse("1/11/2004 23:10:30");
    
    TimeSpan myTimeSpan = new TimeSpan(1, 2, 4, 10);
    DateTime myDateTime8 = myDateTime - myTimeSpan;
    Console.WriteLine("myDateTime - myTimeSpan = " + myDateTime8);
  }
}
myDateTime - myTimeSpan = 31/10/2004 9:06:20 PM

Use the ToFileTime() method to convert a DateTime to an operating system file timestamp

using System;
class MainClass
{
  public static void Main()
  {
    DateTime myDateTime10 = new DateTime(2004, 1, 15, 23, 2, 5);
    long myFileTime = myDateTime10.ToFileTime();
    Console.WriteLine("myDateTime10.ToFileTime() = " + myDateTime10.ToFileTime());
  }
}
myDateTime10.ToFileTime() = 127187101250000000