Get Dates By Min And Max - CSharp System

CSharp examples for System:DateTime Calculate

Description

Get Dates By Min And Max

Demo Code


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;
      }
}

Related Tutorials