Csharp/C Sharp by API/System/TimeSpan — различия между версиями

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

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

new TimeSpan(days, hours, minutes, seconds)

  
using System;
class MainClas
{
  public static void Main()
  {
    
    int hours = 4;
    int minutes = 12;
    int seconds = 10;
    int days = 1;
    TimeSpan myTimeSpan2 = new TimeSpan(days, hours, minutes, seconds);
    Console.WriteLine("myTimeSpan2 = " + myTimeSpan2);
  }
}


new TimeSpan(days, hours, minutes, seconds, milliseconds

  
using System;
class MainClas
{
  public static void Main()
  {
    
    int hours = 4;
    int minutes = 12;
    int seconds = 10;
    int days = 1;
    int milliseconds = 20;
    TimeSpan myTimeSpan3 = new TimeSpan(days, hours, minutes, seconds, milliseconds);
    Console.WriteLine("myTimeSpan3 = " + myTimeSpan3);
  }
}


new TimeSpan(ticks)

  
using System;
class MainClas
{
  public static void Main()
  {
    
    
    long ticks = 300;
    TimeSpan myTimeSpan4 = new TimeSpan(ticks);
    Console.WriteLine("myTimeSpan4 = " + myTimeSpan4);
  }
}


TimeSpan.Add(TimeSpan ts)

  
using System;
class MainClas
{
  public static void Main()
  {
    TimeSpan myTimeSpan13 = new TimeSpan(1, 10, 13);
    TimeSpan myTimeSpan14 = new TimeSpan(2, 6, 10);
    TimeSpan myTimeSpan15 = myTimeSpan13.Add(myTimeSpan14);
    Console.WriteLine("myTimeSpan13 = " + myTimeSpan13);
    Console.WriteLine("myTimeSpan14 = " + myTimeSpan14);
    Console.WriteLine("myTimeSpan15 = " + myTimeSpan15);
  }
}


TimeSpan.Duration()

  
using System;
class MainClas
{
  public static void Main()
  {
    TimeSpan myTimeSpan13 = new TimeSpan(1, 10, 13);
    TimeSpan myTimeSpan14 = new TimeSpan(2, 6, 10);
    TimeSpan myTimeSpan15 = myTimeSpan13.Subtract(myTimeSpan14);
    
    Console.WriteLine("myTimeSpan15.Duration() = " + myTimeSpan15.Duration());
  }
}


TimeSpan.FromDays

  

using System;
class MainClas
{
  public static void Main()
  {
    TimeSpan myTimeSpan5 = TimeSpan.FromDays(5);
    Console.WriteLine("TimeSpan.FromDays(5) = " + myTimeSpan5);
    TimeSpan myTimeSpan6 = TimeSpan.FromHours(10);
    Console.WriteLine("TimeSpan.FromHours(10) = " + myTimeSpan6);
    TimeSpan myTimeSpan7 = TimeSpan.FromMinutes(30);
    Console.WriteLine("TimeSpan.FromMinutes(30) = " + myTimeSpan7);
    TimeSpan myTimeSpan8 = TimeSpan.FromSeconds(15);
    Console.WriteLine("TimeSpan.FromSeconds(15) = " + myTimeSpan8);
    TimeSpan myTimeSpan9 = TimeSpan.FromMilliseconds(200);
    Console.WriteLine("TimeSpan.FromMilliseconds(200) = " + myTimeSpan9);
    TimeSpan myTimeSpan10 = TimeSpan.FromTicks(500);
    Console.WriteLine("TimeSpan.FromTicks(500) = " + myTimeSpan10);
  }
}


TimeSpan.Milliseconds

  
using System;
class MainClass
{
  public static void Main()
  {
    DateTime start = DateTime.Now;
    long total = 0;
    for (int count = 0; count < 1000000; count++)
    {
      total += count;
    }
    TimeSpan timeTaken = DateTime.Now - start;
    Console.WriteLine("Milliseconds = " + timeTaken.Milliseconds);
    Console.WriteLine("total = " + total);
  }
}


TimeSpan.Negate()

  
using System;
class MainClas
{
  public static void Main()
  {
    TimeSpan myTimeSpan13 = new TimeSpan(1, 10, 13);
    TimeSpan myTimeSpan14 = new TimeSpan(2, 6, 10);
    TimeSpan myTimeSpan15 = myTimeSpan13.Subtract(myTimeSpan14);
    
    
    Console.WriteLine("myTimeSpan15.Negate() = " + myTimeSpan15.Negate());
    Console.WriteLine("myTimeSpan14.Negate() = " + myTimeSpan14.Negate());

  }
}


TimeSpan.Parse

  
using System;
class MainClas
{
  public static void Main()
  {
    
    TimeSpan myTimeSpan11 = TimeSpan.Parse("8:10:30");
    Console.WriteLine("TimeSpan.Parse(\"8:10:30\") = " + myTimeSpan11);
  }
}


TimeSpan.Subtract(TimeSpan ts)

  

using System;
class MainClas
{
  public static void Main()
  {
    TimeSpan myTimeSpan13 = new TimeSpan(1, 10, 13);
    TimeSpan myTimeSpan14 = new TimeSpan(2, 6, 10);
    TimeSpan myTimeSpan15 = myTimeSpan13.Subtract(myTimeSpan14);
    Console.WriteLine("myTimeSpan15 = " + myTimeSpan15);
  }
}


TimeSpan.TicksPerDay

  
using System;
using System.Globalization;
class MainClass {
    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);
        // 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());
    }
}


TimeSpan + TimeSpan

  
using System;
class MainClass
{
    public static void Main(string[] args)
    {
        TimeSpan timespan1 = new TimeSpan(2, 12, 0, 0);
        // Create a TimeSpan representing 4.5 days.
        TimeSpan timespan2 = new TimeSpan(4, 12, 0, 0);
        // Create a TimeSpan representing 1 week.
        TimeSpan oneWeek = timespan1 + timespan2;
        
        Console.WriteLine("oneWeek   : {0}", oneWeek);
    
    }
}


TimeSpan.TotalDays

  

using System;
class MainClas
{
  public static void Main()
  {
    long ticks = 300;
    TimeSpan myTimeSpan = new TimeSpan(ticks);
    Console.WriteLine("myTimeSpan.Days = " + myTimeSpan.Days);
    Console.WriteLine("myTimeSpan.Hours = " + myTimeSpan.Hours);
    Console.WriteLine("myTimeSpan.Minutes = " + myTimeSpan.Minutes);
    Console.WriteLine("myTimeSpan.Seconds = " + myTimeSpan.Seconds);
    Console.WriteLine("myTimeSpan.Milliseconds = " + myTimeSpan.Milliseconds);
    Console.WriteLine("myTimeSpan.Ticks = " + myTimeSpan.Ticks);
    Console.WriteLine("myTimeSpan.TotalDays = " + myTimeSpan.TotalDays);
    Console.WriteLine("myTimeSpan.TotalHours = " + myTimeSpan.TotalHours);
    Console.WriteLine("myTimeSpan.TotalMinutes = " + myTimeSpan.TotalMinutes);
    Console.WriteLine("myTimeSpan.TotalSeconds = " + myTimeSpan.TotalSeconds);
    Console.WriteLine("myTimeSpan.TotalMilliseconds = " + myTimeSpan.TotalMilliseconds);
    
  }
}