C#基础:TimeSpan 示例

作者:微信公众号:【架构师老卢】
8-3 18:21
23

概述:C# 语言中的时间跨度#C# 中的 TimeSpan 用于表示持续时间或时间间隔。C# 中的 TimeSpan 提供了一种处理时间相关值的便捷方法,例如两个日期和时间之间的差异。C# 中的 TimeSpan 是 System 命名空间的一部分,它提供了各种属性和方法,用于执行涉及时间间隔的计算。C# 中 timepsan 的功能:在 C# 中创建 TimeSpan:您可以通过多种方式在 C# 中创建 TimeSpan,包括使用构造函数和从字符串进行分析例using System; // Using a constructor to create a TimeSpan TimeSpan ti

C# 语言中的时间跨度#

C# 中的 TimeSpan 用于表示持续时间或时间间隔。C# 中的 TimeSpan 提供了一种处理时间相关值的便捷方法,例如两个日期和时间之间的差异。C# 中的 TimeSpan 是 System 命名空间的一部分,它提供了各种属性和方法,用于执行涉及时间间隔的计算。

C# 中 timepsan 的功能:

在 C# 中创建 TimeSpan:

您可以通过多种方式在 C# 中创建 TimeSpan,包括使用构造函数和从字符串进行分析

using System;  
// Using a constructor to create a TimeSpan  
TimeSpan timeSpan1 = new TimeSpan(3, 2, 30, 15); // 3 days, 2 hours, 30 minutes, 15 seconds  
// Parsing a TimeSpan from a string  
TimeSpan timeSpan2 = TimeSpan.Parse("5.12:45:30.500"); // 5 days, 12 hours, 45 minutes, 30.5 seconds  

在 C# 中访问 TimeSpan 组件:

您可以使用其属性在 C# 中访问 TimeSpan 的各种组件,例如天、小时、分钟、秒和毫秒:

int days = timeSpan1.Days; // 3  
int hours = timeSpan1.Hours; // 2  
int minutes = timeSpan1.Minutes; // 30  
int seconds = timeSpan1.Seconds; // 15  
int milliseconds = timeSpan1.Milliseconds; // 0  

C# 中 TimeSpan 的算术运算:

您可以在 C# 中对 TimeSpan 对象执行算术运算,例如加法和减法:。

TimeSpan total = timeSpan1 + timeSpan2; // Add two TimeSpans  
TimeSpan difference = timeSpan1 - timeSpan2; // Subtract two TimeSpans  

C# 中 TimeSpan 的比较:

您可以使用比较运算符(例如,<,>、<=、>=)来比较 C# 中的 TimeSpan 对象,以检查哪个更大或更小:

bool isGreaterThan = timeSpan1 > timeSpan2; // Compare two TimeSpans  

C 语言中的 TimeSpan ToString#

TimeSpan 提供了 ToString 方法,并支持自定义格式说明符以不同格式表示时间间隔:

string formatted = timeSpan1.ToString(); // Default format  
string customFormatted = timeSpan1.ToString("dd\\\\:hh\\\\:mm\\\\:ss"); // Custom format  

C# 语言中的 TimeSpan 属性和方法

TimeSpan 还为各种操作提供了多种属性和方法,例如 TotalDays、TotalHours、TotalMinutes、TotalSeconds 和 TotalMilliseconds,用于获取不同单位的总时间间隔。

double totalDays = timeSpan1.TotalDays;  
double totalHours = timeSpan1.TotalHours;  
double totalMinutes = timeSpan1.TotalMinutes;  
double totalSeconds = timeSpan1.TotalSeconds;  
double totalMilliseconds = timeSpan1.TotalMilliseconds;  

C# 中的 TimeSpan:示例

using System;  
public class Program  
{  
  public static void Main(string[] args)  
  {  
      // Using a constructor to create a TimeSpan  
      TimeSpan timeSpan1 = new TimeSpan(3, 2, 30, 15); // 3 days, 2 hours, 30 minutes, 15 seconds  
      // Parsing a TimeSpan from a string  
      TimeSpan timeSpan2 = TimeSpan.Parse("5.12:45:30.500"); // 5 days, 12 hours, 45 minutes, 30.5 seconds  
      int days = timeSpan1.Days; // 3  
      int hours = timeSpan1.Hours; // 2  
      int minutes = timeSpan1.Minutes; // 30  
      int seconds = timeSpan1.Seconds; // 15;  
      int milliseconds = timeSpan1.Milliseconds; // 0  
      Console.WriteLine("Time Span Days = {0}", days);  
      Console.WriteLine("Time Span Hours = {0}", hours);  
      Console.WriteLine("Time Span Minutes = {0}", minutes);  
      Console.WriteLine("Time Span Seconds = {0}", seconds);  
      Console.WriteLine("Time Span MilliSeconds = {0}", milliseconds);  
      TimeSpan total = timeSpan1 + timeSpan2; // Add two TimeSpans  
      TimeSpan difference = timeSpan1 - timeSpan2; // Subtract two TimeSpans  
      Console.WriteLine("The Sum of two TimeSpans= {0}", total);  
      Console.WriteLine("The Difference of two TimeSpans = {0}", difference);  
      bool isGreaterThan = timeSpan1 > timeSpan2; // Compare two TimeSpans  
      Console.WriteLine("Compare two TimeSpans = {0}",isGreaterThan);  
      string formatted = timeSpan1.ToString(); // Default format  
      string customFormatted = timeSpan1.ToString("dd\\\\:hh\\\\:mm\\\\:ss"); // Custom format  
      Console.WriteLine("The Default Format is = {0}",formatted);  
      Console.WriteLine("The Custom Format is = {0}", customFormatted);  
      double totalDays = timeSpan1.TotalDays;  
      double totalHours = timeSpan1.TotalHours;  
      double totalMinutes = timeSpan1.TotalMinutes;  
      double totalSeconds = timeSpan1.TotalSeconds;  
      double totalMilliseconds = timeSpan1.TotalMilliseconds;  
      Console.WriteLine("The Total Days are = {0}", totalDays);  
      Console.WriteLine("The Total Hours are = {0}", totalHours);  
      Console.WriteLine("The Total Minutes are = {0}", totalMinutes);  
      Console.WriteLine("The Total Seconds are = {0}", totalSeconds);  
      Console.WriteLine("The Total MilliSecond are = {0}", totalMilliseconds);  
  }  
}

输出:

Time Span Days = 3  
Time Span Hours = 2  
Time Span Minutes = 30  
Time Span Seconds = 15  
Time Span MilliSeconds = 0  
The Sum of two TimeSpans= 8.15:15:45.5000000  
The Difference of two TimeSpans = \-2.10:15:15.5000000  
Compare two TimeSpans = False  
The Default Format is = 3.02:30:15  
The Custom Format is = 03:02:30:15  
The Total Days are = 3.1043402777777778  
The Total Hours are = 74.50416666666666  
The Total Minutes are = 4470.25  
The Total Seconds are = 268215  
The Total MilliSecond are = 268215000

C# 中的 TimeSpan 可用于各种与时间相关的操作,包括计算日期之间的差异、测量持续时间以及处理应用程序中的超时或间隔。C# 中的 TimeSpan 允许您以灵活且准确的方式处理时间。

阅读排行