陈宝刚---享受生活,追逐梦想!
理想是心中的火焰,有追求的人才是幸福的人!
C#中时间差的计算
程序代码: 
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;
posted on 2009-03-07 21:35  追梦人RUBY  阅读(575)  评论(0编辑  收藏  举报