LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

C#基础:TimeSpan 示例

admin
2024年8月23日 23:50 本文热度 372

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 允许您以灵活且准确的方式处理时间。


该文章在 2024/8/24 12:12:47 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved