C#使用TimeSpan對(duì)象實(shí)現(xiàn)獲取時(shí)間間隔
一、TimeSpan基礎(chǔ)知識(shí)
使用TimeSpan對(duì)象可以方便地獲取兩個(gè)時(shí)間段的間隔。兩個(gè)時(shí)間信息相減后會(huì)得到一個(gè)TimeSpan對(duì)象,該TimeSpan對(duì)象代表時(shí)間間隔,可以通過(guò)TimeSpan對(duì)象的Days、Hours、Minutes、Seconds、Milliseconds屬性分別得到間隔的天、時(shí)、分、秒、毫秒數(shù)。
TimeSpan對(duì)象代表兩個(gè)時(shí)間段的間隔或跨度,使用TimeSpan對(duì)象可以方便地獲取兩個(gè)時(shí)間段的間隔。兩個(gè)時(shí)間信息相減后會(huì)得到一個(gè)TimeSpan對(duì)象,該TimeSpan對(duì)象代表時(shí)間間隔,可以通過(guò)TimeSpan對(duì)象的Days、Hours、Minutes、Seconds、Milliseconds屬性分別得到間隔的天、時(shí)、分、秒、毫秒數(shù)。
可以調(diào)用TimeSpan的Add方法,得到兩個(gè)TimeSpan持續(xù)時(shí)間的和。
二、實(shí)例
使用TimeSpan對(duì)象獲取時(shí)間間隔。
// 使用TimeSpan對(duì)象獲取時(shí)間間隔 namespace _064 { public partial class Form1 : Form { private GroupBox? groupBox1; private GroupBox? groupBox2; private GroupBox? groupBox3; private Button? button1; private Button? button2; private Button? button3; private Label? label1; private Label? label2; private Label? label3; public DateTime DateTime_First,DateTime_Second; public Form1() { InitializeComponent(); Load += Form1_Load; } private void Form1_Load(object? sender, EventArgs e) { // // button1 // button1 = new Button { Location = new Point(108, 22), Name = "button1", Size = new Size(106, 23), TabIndex = 0, Text = "第一次獲取時(shí)間", UseVisualStyleBackColor = true }; button1.Click += Button1_Click; // // button2 // button2 = new Button { Location = new Point(108, 22), Name = "button2", Size = new Size(108, 23), TabIndex = 0, Text = "第二次獲取時(shí)間", UseVisualStyleBackColor = true }; button2.Click += Button2_Click; // // button3 // button3 = new Button { Location = new Point(106, 22), Name = "button3", Size = new Size(108, 23), TabIndex = 0, Text = "計(jì)算時(shí)間間隔", UseVisualStyleBackColor = true }; button3.Click += Button3_Click; // // label1 // label1 = new Label { AutoSize = true, Location = new Point(26, 47), Name = "label1", Size = new Size(43, 17), TabIndex = 1, Text = "label1" }; // // label2 // label2 = new Label { AutoSize = true, Location = new Point(26, 50), Name = "label2", Size = new Size(43, 17), TabIndex = 1, Text = "label2" }; // // label3 // label3 = new Label { AutoSize = true, Location = new Point(71, 51), Name = "label3", Size = new Size(43, 17), TabIndex = 1, Text = "label3" }; // // groupBox1 // groupBox1 = new GroupBox { Location = new Point(12, 12), Name = "groupBox1", Size = new Size(315, 75), TabIndex = 0, TabStop = false, Text = "第一次獲取時(shí)間" }; groupBox1.Controls.Add(label1); groupBox1.Controls.Add(button1); groupBox1.SuspendLayout(); // // groupBox2 // groupBox2 = new GroupBox { Location = new Point(12, 94), Name = "groupBox2", Size = new Size(315, 75), TabIndex = 1, TabStop = false, Text = "第二次獲取時(shí)間" }; groupBox2.Controls.Add(label2); groupBox2.Controls.Add(button2); groupBox2.SuspendLayout(); // // groupBox3 // groupBox3 = new GroupBox { Location = new Point(12, 176), Name = "groupBox3", Size = new Size(315, 75), TabIndex = 2, TabStop = false, Text = "時(shí)間間隔" }; groupBox3.Controls.Add(label3); groupBox3.Controls.Add(button3); groupBox3.SuspendLayout(); // // Form1 // AutoScaleDimensions = new SizeF(7F, 17F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(339, 263); Controls.Add(groupBox3); Controls.Add(groupBox2); Controls.Add(groupBox1); Name = "Form1"; StartPosition = FormStartPosition.CenterScreen; Text = "使用TimeSpan對(duì)象獲取時(shí)間間隔"; groupBox1.ResumeLayout(false); groupBox1.PerformLayout(); groupBox2.ResumeLayout(false); groupBox2.PerformLayout(); groupBox3.ResumeLayout(false); groupBox3.PerformLayout(); } /// <summary> /// 第一次獲取時(shí)間 /// </summary> private void Button1_Click(object? sender, EventArgs e) { DateTime_First = DateTime.Now;//為時(shí)間字段賦值 label1!.Text = "系統(tǒng)時(shí)間:" + DateTime_First.ToString( "yyyy年M月d日 H時(shí)m分s秒 fff毫秒"); } /// <summary> /// 第二次獲取時(shí)間 /// </summary> private void Button2_Click(object? sender, EventArgs e) { DateTime_Second = DateTime.Now;//為時(shí)間字段賦值 label2!.Text = "系統(tǒng)時(shí)間:" + DateTime_Second.ToString( "yyyy年M月d日 H時(shí)m分s秒 fff毫秒"); } /// <summary> /// 計(jì)算時(shí)間間隔 /// </summary> private void Button3_Click(object? sender, EventArgs e) { TimeSpan timespan =//計(jì)算兩個(gè)時(shí)間的時(shí)間間隔 DateTime_First > DateTime_Second ? DateTime_First - DateTime_Second : DateTime_Second - DateTime_First; label3!.Text = string.Format("間隔時(shí)間:{0}天{1}時(shí){2}分{3}秒 {4}毫秒", timespan.Days, timespan.Hours, timespan.Minutes, timespan.Seconds, timespan.Milliseconds); } } }
結(jié)果如下
到此這篇關(guān)于C#使用TimeSpan對(duì)象實(shí)現(xiàn)獲取時(shí)間間隔的文章就介紹到這了,更多相關(guān)C# TimeSpan獲取時(shí)間間隔內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- C#簡(jiǎn)單獲取時(shí)間差的小例子
- C#計(jì)算兩個(gè)時(shí)間差的方法代碼分享
- C#基于TimeSpan實(shí)現(xiàn)倒計(jì)時(shí)效果的方法
- c# 計(jì)算時(shí)間間隔的簡(jiǎn)單方法(推薦)
- 詳解C# TimeSpan 計(jì)算時(shí)間差(時(shí)間間隔)
- C#使用TimeSpan時(shí)間計(jì)算的簡(jiǎn)單實(shí)現(xiàn)
- C#的TimeSpan案例詳解
- C#日期時(shí)間類(lèi)的使用方法(DateTime類(lèi)、TimeSpan類(lèi)與DateTimeOffset類(lèi))
相關(guān)文章
C#實(shí)現(xiàn)打開(kāi)畫(huà)圖的同時(shí)載入圖片、最大化顯示畫(huà)圖窗體的方法
這篇文章主要介紹了C#實(shí)現(xiàn)打開(kāi)畫(huà)圖的同時(shí)載入圖片、最大化顯示畫(huà)圖窗體的方法,涉及C#針對(duì)窗體及圖片操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08C#程序連接數(shù)據(jù)庫(kù)及讀取數(shù)據(jù)庫(kù)中字段的簡(jiǎn)單方法總結(jié)
包括C#連接Access、Oracle或者SQL Server,這里整理了一些C#連接數(shù)據(jù)庫(kù)及從讀取數(shù)據(jù)庫(kù)中字段的簡(jiǎn)單方法總結(jié),需要的朋友可以參考下2016-05-05C#實(shí)現(xiàn)身份證驗(yàn)證功能的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)身份證驗(yàn)證功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12基于C#實(shí)現(xiàn)語(yǔ)音識(shí)別功能詳解
在.NET4.0中,可以借助System.Speech組件讓電腦來(lái)識(shí)別我們的聲音。本文將利用該組件實(shí)現(xiàn)語(yǔ)音識(shí)別功能,文中實(shí)現(xiàn)過(guò)程講解詳細(xì),需要的可以參考一下2022-04-04C#使用Newtonsoft.Json中的JObject對(duì)象
本文詳細(xì)講解了C#使用Newtonsoft.Json中JObject對(duì)象的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07C#數(shù)據(jù)結(jié)構(gòu)與算法揭秘一
本文一介紹了數(shù)據(jù)結(jié)構(gòu)的基本概念 而介紹了算法的基本概念,并且重點(diǎn)討論了算法時(shí)間復(fù)雜度,并且用程序予以證明2012-11-11Unity?UGUI的StandaloneInputModule標(biāo)準(zhǔn)輸入模塊組件使用示例
這篇文章主要為大家介紹了Unity?UGUI的StandaloneInputModule標(biāo)準(zhǔn)輸入模塊組件使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08