目录
一、计算时间间隔的方法
1.用DateAndTime.DateDiff方法计算时间间隔
2.使用TimeSpan获取日期时间间隔
二、实例
1.示例一:用DateAndTime.DateDiff方法计算时间间隔
2.示例二:使用TimeSpan获取日期时间间隔
一、计算时间间隔的方法
1.用DateAndTime.DateDiff方法计算时间间隔
在程序设计过程中,经常需要计算两个时间信息所间隔的天数,使用DateDiff方法方便地计算出两个时间段的间隔。
2.使用TimeSpan获取日期时间间隔
使用TimeSpan也可以获取日期时间的间隔数,首先将两个DateTime对象相减,此时会返回TimeSpan对象,然后调用TimeSpan对象的Days属性就可以方便地得到两个DateTime对象所间隔的天数。
TimeSpan表示一个时间间隔。一个TimeSpan对象都表示一个时间间隔 (持续时间的时间或时间),单位为正数或负数的天数、 小时、 分钟、 秒和小数部分的第二个数字。
//主要属性
Days:Int32 此实例的天数部分。 返回值可以是正数也可以是负数。
Hours:Int32 当前 TimeSpan 结构的小时数部分。 返回值的范围为 -23 到 23。
Milliseconds:Int32 当前 TimeSpan 结构的毫秒数部分。 返回值的范围为 -999 到 999。
Minutes:Int32 当前 TimeSpan 结构的分钟数部分。 返回值的范围为 -59 到 59。
Seconds:Int32 当前 TimeSpan 结构的秒数部分。 返回值的范围为 -59 到 59。
Ticks:Int64 此实例包含的刻度数。
TotalDays:Double 此实例表示的总天数。
TotalHours:Double 此实例表示的总小时数。
TotalMilliseconds:Double 此实例表示的总毫秒数。
TotalMinutes:Double 此实例表示的总分钟数。
TotalSeconds:Double 此实例表示的总秒数。
二、实例
1.示例一:用DateAndTime.DateDiff方法计算时间间隔
// DateAndTime.DateDiff()
using Microsoft.VisualBasic;namespace _062
{public partial class Form1 : Form{private GroupBox? groupBox1;private DateTimePicker? dateTimePicker2;private DateTimePicker? dateTimePicker1;private Label? label2;private Label? label1;private Button? button1;public Form1(){InitializeComponent();Load += Form1_Load;}private void Form1_Load(object? sender, EventArgs e){// // dateTimePicker2// dateTimePicker2 = new DateTimePicker{Location = new Point(123, 49),Name = "dateTimePicker2",Size = new Size(200, 23),TabIndex = 3};// // dateTimePicker1// dateTimePicker1 = new DateTimePicker{Location = new Point(123, 25),Name = "dateTimePicker1",Size = new Size(200, 23),TabIndex = 2};// // label2// label2 = new Label{AutoSize = true,Location = new Point(6, 55),Name = "label2",Size = new Size(104, 17),TabIndex = 1,Text = "第二次输入时间:"};// // label1// label1 = new Label{AutoSize = true,Location = new Point(6, 31),Name = "label1",Size = new Size(104, 17),TabIndex = 0,Text = "第一次输入时间:"};// // button1// button1 = new Button{Location = new Point(135, 104),Name = "button1",Size = new Size(75, 23),TabIndex = 2,Text = "计算间隔",UseVisualStyleBackColor = true};button1.Click += Button1_Click;// // groupBox1// groupBox1 = new GroupBox{Location = new Point(12, 12),Name = "groupBox1",Size = new Size(330, 86),TabIndex = 0,TabStop = false,Text = "求时间间隔"};groupBox1.Controls.Add(dateTimePicker2);groupBox1.Controls.Add(dateTimePicker1);groupBox1.Controls.Add(label2);groupBox1.Controls.Add(label1);groupBox1.SuspendLayout();// // Form1// AutoScaleDimensions = new SizeF(7F, 17F);AutoScaleMode = AutoScaleMode.Font;ClientSize = new Size(354, 136);Controls.Add(button1);Controls.Add(groupBox1);Name = "Form1";StartPosition = FormStartPosition.CenterScreen;Text = "计算时间间隔"; groupBox1.ResumeLayout(false);groupBox1.PerformLayout();}/// <summary>/// 使用DateDiff方法获取日期间隔/// </summary>private void Button1_Click(object? sender, EventArgs e){MessageBox.Show("间隔 " +DateAndTime.DateDiff(DateInterval.Day, dateTimePicker1!.Value, dateTimePicker2!.Value,FirstDayOfWeek.Sunday, FirstWeekOfYear.Jan1).ToString() + " 天", "间隔时间");}}
}
2.示例二:使用TimeSpan获取日期时间间隔
// 用TimeSpan对象的方法计算时间间隔
namespace _062_1
{internal class Program{private static void Main(string[] args){ArgumentNullException.ThrowIfNull(args);DateTime date1 = new(2023, 1, 1, 8, 0, 15); DateTime date2 = new(2024, 8, 18, 13, 30, 30);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);}}
}
// 运行结果:
/*
2024-08-18 13:30:30 - 2023-01-01 08:00:15 = 595.05:30:15Value of Days Component: 595Total Number of Days: 595.2293402777777Value of Hours Component: 5Total Number of Hours: 14285.504166666668Value of Minutes Component: 30Total Number of Minutes: 857130.25Value of Seconds Component: 15Total Number of Seconds: 51,427,815Value of Milliseconds Component: 0Total Number of Milliseconds: 51,427,815,000Ticks: 514,278,150,000,000*/