C# 时间函数相减 – 饼子天空 – 博客园

1:第一种方式:

?
1
2
3
4
5
6
string firstDate = "20140226120201";
string secondDate = "20140225115401";
DateTime firstDateTemp = DateTime.ParseExact(firstDate, "yyyyMMddHHmmss", new System.Globalization.CultureInfo("zh-CN", true));
DateTime secondDateTemp = DateTime.ParseExact(secondDate, "yyyyMMddHHmmss", new System.Globalization.CultureInfo("zh-CN", true));           
TimeSpan span = firstDateTemp.Subtract(secondDateTemp);
然后,如果相差一天,那么span.Days=1,如果是一年,那么span.Days=365<br>             span.Hours, span.Minutes就不举例了

2:第二种方式:

?
1
2
3
DateTime t1 = DateTime.Parse("2007-1-1 05:32:22");
    DateTime t2 = DateTime.Parse("2007-1-1 05:31:22");
    TimeSpan span = t1.Subtract(t2);

来源URL:http://www.cnblogs.com/bingzisky/p/3568867.html