Csharp/C Sharp by API/System/DateTime

Материал из .Net Framework эксперт
Версия от 12:12, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

DateTime.AddSeconds

    
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());
        }
   }
}


DateTime.Add(TimeSpan)

   
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 myDateTime7 = myDateTime.Add(myTimeSpan);
    Console.WriteLine("myTimeSpan = " + myTimeSpan);
    Console.WriteLine("myDateTime6.Add(myTimeSpan) = " + myDateTime7);
  }
}


DateTime.AddYears

    
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);
  }
}


DateTime.Compare(DateTime first, DateTime second)

    
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));
  }
}


DateTime.Date

  
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);
    }
}


DateTime

    
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);
  }
}


DateTime.DayOfWeek

    
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);

  }
}


DateTime.DaysInMonth

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


DateTime.Equals(DateTime dt)

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


DateTime.Equals(DateTime first, DateTime second)

    
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);
  }
}


DateTime.FromFileTime

    
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.IsDaylightSavingTime()

  
using System;
using System.Collections.Generic;
using System.Text;
class Program {
    static void Main(string[] args) {
        DateTime dt = new DateTime(2004, 10, 17);
        dt.AddMonths(2);  // Month is now December.
        Console.WriteLine("Daylight savings: {0}", dt.IsDaylightSavingTime());
    }
}


DateTime.IsLeapYear

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


DateTime.MaxValue

  

using System;
using System.Globalization;
class MainClass {
    public static void Main() {
        
        Console.WriteLine("Min Date: {0}", DateTime.MinValue.ToString());
        Console.WriteLine("Max Date: {0}", DateTime.MaxValue.ToString());
    }
}


DateTime.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()
  {
    
    DateTime myDateTime4 = new DateTime(0);
    TimeSpan myTimeSpan = new TimeSpan(4, 12, 10);
    myDateTime4 += myTimeSpan;
    DisplayDateTime("myDateTime4", myDateTime4);
    
    
  }
}


DateTime.Now

    
using System;
class MainClass
{
    public static void Main(string[] args)
    {
        DateTime now = DateTime.Now;
        Console.WriteLine("Now   : {0}", now);
    }
}


DateTime.Parse

    
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);
  }
}


DateTime.ParseExact

   
using System;
class MainClass
{
    public static void Main(string[] args)
    {
        DateTime dt6 = DateTime.ParseExact("2:13:30 PM", "h:mm:ss tt", null);
        Console.WriteLine(dt6);
    }
}


DateTime.Second

   
using System;  
  
class MainClass {  
  public static void Main() {  
    string t; 
    int seconds; 
 
    DateTime dt = DateTime.Now; 
    seconds = dt.Second; 
 
    if(seconds != dt.Second) { 
      seconds = dt.Second; 
 
      t = dt.ToString("T"); 
      Console.WriteLine(t); 
    } 
  } 
}


DateTime.Subtract(TimeSpan t)

   
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 myDateTime7 = myDateTime.Subtract(myTimeSpan);
    Console.WriteLine("myTimeSpan = " + myTimeSpan);
    Console.WriteLine("myDateTime6.Subtract(myTimeSpan) = " + myDateTime7);
  }
}


DateTime.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() {
        int year = 2008;
        int month = 8;
        int day = 25;
        DateTime myDateTime = new DateTime(year, month, day);
        int hour = 23;
        int minute = 30;
        int second = 12;
        int millisecond = 5;
        DateTime myDateTime2 = new DateTime(year, month, day, hour, minute, second, millisecond);
        System.Globalization.JulianCalendar myCalendar = new System.Globalization.JulianCalendar();
        DateTime myDateTime3 = new DateTime(year, month, day, myCalendar);
        DateTime myDateTime4 = new DateTime(0);
        DisplayDateTime("myDateTime", myDateTime);
        DisplayDateTime("myDateTime2", myDateTime2);
        DisplayDateTime("myDateTime3", myDateTime3);
        DisplayDateTime("myDateTime4", myDateTime4);
        TimeSpan myTimeSpan = new TimeSpan(4, 12, 10);
        myDateTime4 += myTimeSpan;
        DisplayDateTime("myDateTime4", myDateTime4);
    }
}


DateTime.TimeOfDay

    
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);

  }
}


DateTime - TimeSpan

    
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);
  }
}


DateTime.Today

  
using System;
using System.Globalization;
class MainClass {
    public static void Main() {
        
        Console.WriteLine("Current Date and Time: {0}", DateTime.Now.ToString());
        Console.WriteLine("Current Date Only: {0}", DateTime.Today.ToString());
    }
}


DateTime.ToFileTime

    
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());
  }
}


DateTime.ToLongDateString()

    
using System;
class MainClass
{
  public static void Main()
  {
    DateTime myDateTime11 = new DateTime(2004, 1, 15, 23, 2, 5);
    Console.WriteLine("myDateTime11 = " + myDateTime11);
    Console.WriteLine("myDateTime11.ToLongDateString() = " + myDateTime11.ToLongDateString());
    Console.WriteLine("myDateTime11.ToShortDateString() = " + myDateTime11.ToShortDateString());

  }
}


DateTime.ToLongTimeString()

 
using System;
using System.Globalization;
using System.Threading;
class Class1 {
  static void Main(string[] args) {
      CultureInfo MyCulture = new CultureInfo("fr-FR");
      DateTime MyDate = System.DateTime.Now;
      Thread.CurrentThread.CurrentCulture = MyCulture;
      Console.WriteLine(MyDate.ToLongTimeString());
      Console.WriteLine(MyDate.ToShortDateString());
      
   }
}


DateTime.ToShortDateString()

 
using System;
using System.Globalization;
using System.Threading;
class Class1 {
  static void Main(string[] args) {
      CultureInfo MyCulture = new CultureInfo("fr-FR");
      DateTime MyDate = System.DateTime.Now;
      Thread.CurrentThread.CurrentCulture = MyCulture;
      Console.WriteLine(MyDate.ToLongTimeString());
      Console.WriteLine(MyDate.ToShortDateString());
      
   }
}


DateTime.ToShortTimeString()

    
using System;
class MainClass
{
  public static void Main()
  {
    DateTime myDateTime11 = new DateTime(2004, 1, 15, 23, 2, 5);
    Console.WriteLine("myDateTime11 = " + myDateTime11);
    Console.WriteLine("myDateTime11.ToLongTimeString() = " +  myDateTime11.ToLongTimeString());
    Console.WriteLine("myDateTime11.ToShortTimeString() = " +  myDateTime11.ToShortTimeString());
  }
}


DateTime.ToString(String format)

    
using System;
class MainClass
{
  public static void Main()
  {
    DateTime myDateTime = new DateTime(2004, 1, 12, 22, 2, 10);
    Console.WriteLine("myDateTime.ToString() = " + myDateTime.ToString());
    Console.WriteLine("myDateTime.ToString(\"MMMM dd, yyyy\") = " + myDateTime.ToString("MMMM dd, yyyy"));
  }
}


DateTime.ToString(String format, CultureInfo ci)

 
using System;
using System.Globalization;
public class Example21_10 
{
  public static void Main() 
  {
    // create a date and a currency value
    DateTime dtNow = DateTime.Now;
    Double curOriginal = 12345.67;
    // and format the variables for a specific culture
    CultureInfo ci = new CultureInfo("en-US");
    string sLocalizedDate = dtNow.ToString("d", ci);
    string sLocalizedCur = curOriginal.ToString("c", ci);
    // print them out
    Console.WriteLine(sLocalizedDate);
    Console.WriteLine(sLocalizedCur);
    // and format them for a second culture
    CultureInfo ci2 = new CultureInfo("en-GB");
    string sLocalizedDate2 = dtNow.ToString("d", ci2);
    string sLocalizedCur2 = curOriginal.ToString("c", ci2);
    // print them out again
    Console.WriteLine(sLocalizedDate2);
    Console.WriteLine(sLocalizedCur2);
  }
}


DateTime.UtcNow

    
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.Year

    
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);
  }
}


new DateTime

    
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);
    }
}


new DateTime(0)

    
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);
  }
}


new DateTime(year, month, day, hour, minute, second, 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);
  }
}