C#使用TimeSpan对象获取时间间隔

目录

一、TimeSpan基础知识

二、实例


一、TimeSpan基础知识

        使用TimeSpan对象可以方便地获取两个时间段的间隔。两个时间信息相减后会得到一个TimeSpan对象,该TimeSpan对象代表时间间隔,可以通过TimeSpan对象的Days、Hours、Minutes、Seconds、Milliseconds属性分别得到间隔的天、时、分、秒、毫秒数。

        TimeSpan对象代表两个时间段的间隔或跨度,使用TimeSpan对象可以方便地获取两个时间段的间隔。两个时间信息相减后会得到一个TimeSpan对象,该TimeSpan对象代表时间间隔,可以通过TimeSpan对象的Days、Hours、Minutes、Seconds、Milliseconds属性分别得到间隔的天、时、分、秒、毫秒数。 

        可以调用TimeSpan的Add方法,得到两个TimeSpan持续时间的

二、实例

         使用TimeSpan对象获取时间间隔。

// 使用TimeSpan对象获取时间间隔
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 = "第一次获取时间",
                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 = "第二次获取时间",
                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 = "计算时间间隔",
                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 = "第一次获取时间"
            };
            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 = "第二次获取时间"
            };
            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 = "时间间隔"
            };
            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对象获取时间间隔";
            groupBox1.ResumeLayout(false);
            groupBox1.PerformLayout();
            groupBox2.ResumeLayout(false);
            groupBox2.PerformLayout();
            groupBox3.ResumeLayout(false);
            groupBox3.PerformLayout();
        }
        /// <summary>
        /// 第一次获取时间
        /// </summary>
        private void Button1_Click(object? sender, EventArgs e)
        {
            DateTime_First = DateTime.Now;//为时间字段赋值
            label1!.Text = "系统时间:" + DateTime_First.ToString(
                "yyyy年M月d日 H时m分s秒 fff毫秒");
        }
        /// <summary>
        /// 第二次获取时间
        /// </summary>
        private void Button2_Click(object? sender, EventArgs e)
        {
            DateTime_Second = DateTime.Now;//为时间字段赋值
            label2!.Text = "系统时间:" + DateTime_Second.ToString(
                "yyyy年M月d日 H时m分s秒 fff毫秒");
        }
        /// <summary>
        /// 计算时间间隔
        /// </summary>
        private void Button3_Click(object? sender, EventArgs e)
        {
            TimeSpan timespan =//计算两个时间的时间间隔
                DateTime_First > DateTime_Second ?
                DateTime_First - DateTime_Second :
                DateTime_Second - DateTime_First;
            label3!.Text = string.Format("间隔时间:{0}天{1}时{2}分{3}秒 {4}毫秒",
                timespan.Days, timespan.Hours,
                timespan.Minutes, timespan.Seconds,
                timespan.Milliseconds);
        }
    }
}

相关推荐

  1. c/c++】获取时间

    2024-01-28 05:08:01       31 阅读
  2. C# 时间获取

    2024-01-28 05:08:01       29 阅读
  3. c#,获取时间

    2024-01-28 05:08:01       41 阅读
  4. bash计算时间差 时间间隔

    2024-01-28 05:08:01       21 阅读
  5. C语言 获取系统时间

    2024-01-28 05:08:01       13 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-28 05:08:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-28 05:08:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-28 05:08:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-28 05:08:01       20 阅读

热门阅读

  1. 力扣24-两两交换链表中的节点

    2024-01-28 05:08:01       41 阅读
  2. 基于SpringBoot实现策略模式提供系统接口扩展能力

    2024-01-28 05:08:01       36 阅读
  3. LeetCode-题目整理【11】:回溯算法

    2024-01-28 05:08:01       21 阅读
  4. Nginx限流详解

    2024-01-28 05:08:01       37 阅读
  5. Linux的几个常用基本指令2

    2024-01-28 05:08:01       35 阅读
  6. Python 每日学习 7.字符串

    2024-01-28 05:08:01       28 阅读
  7. 大语言模型-任务规划与分解论文

    2024-01-28 05:08:01       34 阅读