CSharp examples for System:DateTime Calculate
Get Dates By Min And Max
using System.Collections.Generic; using System;//from w ww . j a v a2 s. c om public class Main{ public static List<DateTime> GetDatesByMinAndMax(DateTime minDate, DateTime maxDate, int countOfDates) { var minTimeSpan = new TimeSpan(minDate.Date.Ticks); var maxTimeSpan = new TimeSpan(maxDate.Date.Ticks); var diff = maxTimeSpan - minTimeSpan; if (diff.TotalDays < countOfDates) { maxDate = maxDate.AddDays(countOfDates - diff.TotalDays); } minTimeSpan = new TimeSpan(minDate.Ticks); maxTimeSpan = new TimeSpan(maxDate.Ticks); diff = maxTimeSpan - minTimeSpan; var period = diff.TotalDays / (countOfDates - 1); var reportDates = new List<DateTime> { minDate.Date }; var date = minDate.Date; for (var i = 1; i < (countOfDates - 1); i++) { reportDates.Add(date.AddDays(period * i).Date); } reportDates.Add(maxDate.Date); return reportDates; } }