當前位置: 首頁>>代碼示例>>C#>>正文


C# TimeSpan結構體代碼示例

本文整理匯總了C#中System.TimeSpan結構體的典型用法代碼示例。如果您正苦於以下問題:C# TimeSpan結構體的具體用法?C# TimeSpan怎麽用?C# TimeSpan使用的例子?那麽, 這裏精選的結構體代碼示例或許可以為您提供幫助。


TimeSpan結構體屬於System命名空間,在下文中一共展示了TimeSpan結構體的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: DateTime

// Define two dates.
DateTime date1 = new DateTime(2010, 1, 1, 8, 0, 15);
DateTime date2 = new DateTime(2010, 8, 18, 13, 30, 30);

// Calculate the interval between the two dates.
TimeSpan interval = date2 - date1;
Console.WriteLine("{0} - {1} = {2}", date2, date1, interval.ToString());

// Display individual properties of the resulting TimeSpan object.
Console.WriteLine("   {0,-35} {1,20}", "Value of Days Component:", interval.Days);
Console.WriteLine("   {0,-35} {1,20}", "Total Number of Days:", interval.TotalDays);
Console.WriteLine("   {0,-35} {1,20}", "Value of Hours Component:", interval.Hours);
Console.WriteLine("   {0,-35} {1,20}", "Total Number of Hours:", interval.TotalHours);
Console.WriteLine("   {0,-35} {1,20}", "Value of Minutes Component:", interval.Minutes);
Console.WriteLine("   {0,-35} {1,20}", "Total Number of Minutes:", interval.TotalMinutes);
Console.WriteLine("   {0,-35} {1,20:N0}", "Value of Seconds Component:", interval.Seconds);
Console.WriteLine("   {0,-35} {1,20:N0}", "Total Number of Seconds:", interval.TotalSeconds);
Console.WriteLine("   {0,-35} {1,20:N0}", "Value of Milliseconds Component:", interval.Milliseconds);
Console.WriteLine("   {0,-35} {1,20:N0}", "Total Number of Milliseconds:", interval.TotalMilliseconds);
Console.WriteLine("   {0,-35} {1,20:N0}", "Ticks:", interval.Ticks);
開發者ID:.NET開發者,項目名稱:System,代碼行數:20,代碼來源:TimeSpan

輸出:

8/18/2010 1:30:30 PM - 1/1/2010 8:00:15 AM = 229.05:30:15
Value of Days Component:                             229
Total Number of Days:                   229.229340277778
Value of Hours Component:                              5
Total Number of Hours:                  5501.50416666667
Value of Minutes Component:                           30
Total Number of Minutes:                       330090.25
Value of Seconds Component:                           15
Total Number of Seconds:                      19,805,415
Value of Milliseconds Component:                       0
Total Number of Milliseconds:             19,805,415,000
Ticks:                               198,054,150,000,000

示例2: TimeSpan

TimeSpan interval = new TimeSpan(2, 14, 18);
Console.WriteLine(interval.ToString());              

// Displays "02:14:18".
開發者ID:.NET開發者,項目名稱:System,代碼行數:4,代碼來源:TimeSpan

示例3: DateTime

DateTime departure = new DateTime(2010, 6, 12, 18, 32, 0);
DateTime arrival = new DateTime(2010, 6, 13, 22, 47, 0);
TimeSpan travelTime = arrival - departure;  
Console.WriteLine("{0} - {1} = {2}", arrival, departure, travelTime);
開發者ID:.NET開發者,項目名稱:System,代碼行數:4,代碼來源:TimeSpan

輸出:

6/13/2010 10:47:00 PM - 6/12/2010 6:32:00 PM = 1.04:15:00

示例4: Random

Random rnd = new Random();

   TimeSpan timeSpent = TimeSpan.Zero;

   timeSpent += GetTimeBeforeLunch();
   timeSpent += GetTimeAfterLunch();

   Console.WriteLine("Total time: {0}", timeSpent);

   TimeSpan GetTimeBeforeLunch()
   {
      return new TimeSpan(rnd.Next(3, 6), 0, 0);
   }
   
   TimeSpan GetTimeAfterLunch()
   {
      return new TimeSpan(rnd.Next(3, 6), 0, 0);
   }

   // The example displays output like the following:
   //        Total time: 08:00:00
開發者ID:.NET開發者,項目名稱:System,代碼行數:21,代碼來源:TimeSpan

示例5: foreach

string[] values = { "12", "31.", "5.8:32:16", "12:12:15.95", ".12"};
foreach (string value in values)
{
   try {
      TimeSpan ts = TimeSpan.Parse(value);
      Console.WriteLine("'{0}' --> {1}", value, ts);
   }
   catch (FormatException) {
      Console.WriteLine("Unable to parse '{0}'", value);
   }
   catch (OverflowException) {
      Console.WriteLine("'{0}' is outside the range of a TimeSpan.", value);
   }   
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:14,代碼來源:TimeSpan

輸出:

'12' --> 12.00:00:00
Unable to parse '31.'
'5.8:32:16' --> 5.08:32:16
'12:12:15.95' --> 12:12:15.9500000
Unable to parse '.12'

示例6: ShowFormattingCode

ShowFormattingCode();
// Output from .NET Framework 3.5 and earlier versions:
//       12:30:45
// Output from .NET Framework 4:
//       Invalid Format    

Console.WriteLine("---");

ShowParsingCode();
// Output:
//       000000006 --> 6.00:00:00

void ShowFormattingCode()
{
   TimeSpan interval = new TimeSpan(12, 30, 45);
   string output;
   try {
      output = String.Format("{0:r}", interval);
   }
   catch (FormatException) {
      output = "Invalid Format";
   }
   Console.WriteLine(output);
}

void ShowParsingCode()
{
   string value = "000000006";
   try {
      TimeSpan interval = TimeSpan.Parse(value);
      Console.WriteLine("{0} --> {1}", value, interval);
   }
   catch (FormatException) {
      Console.WriteLine("{0}: Bad Format", value);
   }   
   catch (OverflowException) {
      Console.WriteLine("{0}: Overflow", value);
   }
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:39,代碼來源:TimeSpan

示例7: Main

//引入命名空間
using System;

public class Example
{
   public static void Main()
   {
      AppDomainSetup appSetup = new AppDomainSetup();
      appSetup.SetCompatibilitySwitches( new string[] { "NetFx40_TimeSpanLegacyFormatMode" } );
      AppDomain legacyDomain = AppDomain.CreateDomain("legacyDomain", 
                                                      null, appSetup);
      legacyDomain.ExecuteAssembly("ShowTimeSpan.exe");
   }
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:14,代碼來源:TimeSpan

示例8: Main

//引入命名空間
using System;

public class Example
{
   public static void Main()
   {
      TimeSpan interval = DateTime.Now - DateTime.Now.Date;
      string msg = String.Format("Elapsed Time Today: {0:d} hours.",
                                 interval);
      Console.WriteLine(msg);
   }
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:13,代碼來源:TimeSpan

輸出:

Elapsed Time Today: 01:40:52.2524662 hours.

示例9: 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);
  }
}
開發者ID:C#程序員,項目名稱:System,代碼行數:16,代碼來源:TimeSpan

示例10: 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);
  }
}
開發者ID:C#程序員,項目名稱:System,代碼行數:17,代碼來源:TimeSpan

示例11: new TimeSpan(ticks)

//引入命名空間
using System;

class MainClas
{
  public static void Main()
  {
    
    
    long ticks = 300;
    TimeSpan myTimeSpan4 = new TimeSpan(ticks);
    Console.WriteLine("myTimeSpan4 = " + myTimeSpan4);
  }
}
開發者ID:C#程序員,項目名稱:System,代碼行數:14,代碼來源:TimeSpan


注:本文中的System.TimeSpan結構體示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。