程序代码:
label1.Text = "2004-1-1 15:36:05";
label2.Text = "2004-3-1 20:16:35";
DateTime d1 =new DateTime(2004,1,1,15,36,05);
DateTime d2 =new DateTime(2004,3,1,20,16,35);
TimeSpan d3 = d2.Subtract(d1);
label3.Text = "相差:"
+d3.Days.ToString()+"天"
+d3.Hours.ToString()+"小时"
+d3.Minutes.ToString()+"分钟"
+d3.Seconds.ToString()+"秒";
程序代码:
c#的时间段TimeSpan对象的最大间隔单位为天
简单的实现如下:
System.DateTime date1 = new System.DateTime(1996, 6, 3, 22, 15, 0);
System.DateTime date2 = new System.DateTime(1996, 12, 6, 13, 2, 0);
System.DateTime date3 = new System.DateTime(1996, 10, 12, 8, 42, 0);
// diff1 gets 185 days, 14 hours, and 47 minutes.
System.TimeSpan diff1 = date2.Subtract(date1);
// date4 gets 4/9/1996 5:55:00 PM.
System.DateTime date4 = date3.Subtract(diff1);
// diff2 gets 55 days 4 hours and 20 minutes.
System.TimeSpan diff2 = date2 - date3;
// date5 gets 4/9/1996 5:55:00 PM.
System.DateTime date5 = date1 - diff2;
//时间差计算
//TimeSpan span= datetime2.Subtract(datetime1)
//span.TotalSeconds
//TimeSpan ts = DateTime.Now.Subtract(Convert.ToDateTime(MyDS1.Tables[0].Rows[0][2]));
//int miao = ts.TotalSeconds();
TimeSpan t1 = DateTime.Now - Convert.ToDateTime(MyDS1.Tables[0].Rows[0][2]);
int second = ts.TotalSeconds();