C#日期加减

方法一:
       DateTime.Today.AddDays(-1).ToString("yyyy-MM-dd")
方法二:
      TimeSpan span = new DateTime(2005,2,11,22,22,44,555) - new DateTime(2004,9,1,1,1,1,1);
      int int_day=span.Days;
      int int_hour=span.Hours;
      int int_min=span.Minutes;
      int int_sec=span.Seconds;
      int int_millisec=span.Milliseconds;

方法三:


一个日期加上或减去一个值 (比如年份): 
DateTime newDate = DateTime.Now.AddYears(2);    //加 
DateTime newDate = DateTime.Now.AddYears(-2);   //减 
两个日期相减 
DateTime   date1   =   new   DateTime(2007,7,15);    
DateTime   date2 =   DateTime.Now;    
TimeSpan   ts   =   date2 - date1; 
int   iDay=   ts.Days;

posted on 2009-06-22 11:28  Ryan.L.R  阅读(307)  评论(0)    收藏  举报

导航