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#时间计算程序代码:
C#时间计算段TimeSpan对象的***间隔单位为天
C#时间计算的简单实现如下:
- 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;
【编辑推荐】