.net 获取两个时间段中间的日期

private void GetDataTimeInterval(DateTime begin_time, DateTime end_time, ref List<string> list)
      {
           list.Add(begin_time.ToShortDateString());
           if (begin_time.ToShortDateString() != end_time.ToShortDateString())
           {
            begin_time = begin_time.AddDays(1);
            GetDataTimeInterval(begin_time, end_time, ref list);//返回时间区间列表GetDataTimeInterval

}
}

调用:

List<string> list = new List<string>();
GetDataTimeInterval(Convert.ToDateTime(begin_date), Convert.ToDateTime(end_date), ref list);

此时返回的list就是两个日期中间的

比如GetDataTimeInterval(2019-07-01,2019-0704), ref list);

返回的list包含:2019-07-01,2019-07-02,2019-07-03,2019-07-4

猜你喜欢

转载自www.cnblogs.com/ljh19/p/11131378.html
今日推荐