Ejemplo n.º 1
0
        /// <summary>
        /// Gets the scalar difference between two dates given a <see cref="DateInterval"/> value.
        /// </summary>
        /// <param name="interval">The interval to calculate</param>
        /// <param name="start">The start date</param>
        /// <param name="end">The end date</param>
        /// <param name="excludeEndDate">If true, the difference is exclusive of the end date</param>
        /// <returns></returns>
        public static int GetDifference(DateInterval interval, DateTime start, DateTime end, bool excludeEndDate = false)
        {
            var sum = 0;
            var span = new DateSpan(start, end);

            switch (interval)
            {
                case DateInterval.Years:
                    sum += span.Years;
                    break;
                case DateInterval.Months:
                    if (span.Years > 0)
                    {
                        sum += span.Years * 12;
                    }
                    sum += span.Months;
                    sum += span.Weeks / 4; // Helps resolve lower resolution
                    break;
                case DateInterval.Weeks:
                    sum = GetDifferenceInDays(start, span, excludeEndDate) / 7;
                    break;
                case DateInterval.Days:
                    sum = GetDifferenceInDays(start, span, excludeEndDate);
                    break;
                case DateInterval.Hours:
                    sum = GetDifferenceInDays(start, span, excludeEndDate) * 24;
                    sum += span.Hours;
                    break;
                case DateInterval.Minutes:
                    sum = GetDifferenceInDays(start, span, excludeEndDate) * 24 * 60;
                    sum += span.Hours * 60;
                    sum += span.Minutes;
                    break;
                case DateInterval.Seconds:
                    sum = GetDifferenceInDays(start, span, excludeEndDate) * 24 * 60 * 60;
                    sum += span.Hours * 60 * 60;
                    sum += span.Minutes * 60;
                    sum += span.Seconds;
                    break;
                default:
                    throw new ArgumentOutOfRangeException("interval");
            }

            return sum;
        }
Ejemplo n.º 2
0
        private static int GetDifferenceInDays(DateTime start, DateSpan span, bool excludeEndDate = true)
        {
            var sum = 0;

            if (span.Years > 0)
            {
                for (var i = 0; i < span.Years; i++)
                {
                    var year = start.Year + i;
                    sum += DateTime.IsLeapYear(year) ? 366 : 365;
                }
            }

            if (span.Months > 0)
            {
                for (var i = 1; i <= span.Months; i++)
                {
                    var month = start.Month + i;
                    if (month >= 13)
                    {
                        month -= 12;
                    }
                    sum += DateTime.DaysInMonth(start.Year + span.Years, month);
                }
            }

            sum += span.Weeks*7;

            sum += span.Days;
            
            if(excludeEndDate)
            {
                sum--;
            }

            return sum;
        }