当前位置: 首页>>代码示例>>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;未经允许,请勿转载。