c#如何实现时间相加

来源:百度知道 编辑:UC知道 时间:2024/06/18 10:35:41
比如:2008-8-8 加上5 等于 2013-8-8 如何实现呢?
2008-8-8 和5年都不是固定的,那2008-8-8加5中的5需要转化成什么类型呢?

DateTime dt1 = DateTime.Parse("2008-8-8");
DateTime dt2 = dt1.AddYears(5);
时间只要你能得到一个DateTime就行,从年到毫秒都可以加,转成int即可

利用DateTime提供Addxxxx方法实现时间相加或相减



以AddDays方法为例

DateTime t = DateTime.Now;  // t的值为 2015/9/24 9:02:09
// 增加1天
DateTime t1 = t.AddDay(1);  // t1的值为 2015/9/25 9:02