根据时间段查询出范围内所有日期
/// <summary>
/// 根据时间段查询范围内所有日期
/// </summary>
/// <param name="startDateTime">开始时间</param>
/// <param name="EndDateTime">结束时间</param>
/// <returns></returns>
[HttpGet]
[Authorize]
public async Task<MessageModel<List<DateTime>>> GetAllDate(DateTime? startDateTime = null , DateTime? endDateTime = null)
{
MessageModel<List<DateTime>> result = new MessageModel<List<DateTime>>()
{
success = false
};
try
{
List<DateTime> dateTimeList = new List<DateTime>();
DateTime dateTime = new DateTime();
//循环比较,取出日期;
for (dateTime = Convert.ToDateTime(startDateTime); dateTime.CompareTo(Convert.ToDateTime(endDateTime).Date) <= 0; dateTime = dateTime.AddDays(1))
{
dateTimeList.Add(dateTime);
}
result.response = dateTimeList;
result.success = true;
result.msg = "查询成功";
}
catch (Exception ex)
{
result.msg = "获取数据异常" + ex.Message;
}
return result;
}
原文:https://www.cnblogs.com/hugeboke/p/15745079.html