Beispiel #1
0
 public static DateRange ParseString(string value)
 {
     DateRange dateRange = new DateRange();
     dateRange.Parse(value);
     return dateRange;
 }
Beispiel #2
0
        public static DateRange CreateQuarterDateRange(DateTime dateTime)
        {
            int quarterMonth = (dateTime.Month % 3) == 0 ? dateTime.Month : 3 * (dateTime.Month / 3) + 1;

            DateTime quarterDate = new DateTime(dateTime.Year, quarterMonth, 1);
            DateRange dateRange = new DateRange(quarterDate, quarterDate.AddMonths(3)) { Kind = DateRangeKind.Quarter };

            return dateRange;
        }
Beispiel #3
0
        public static DateRange[] CreateForMonthsInRecentQuarters(int range)
        {
            DateTime today = DateTime.UtcNow.Date;
            int quarterMonth = ((today.Month % 3) == 0 ? today.Month : 3 * ((today.Month / 3) + 1));

            DateTime dateTime = new DateTime(DateTime.Today.Year, quarterMonth, 1);

            List<DateRange> dateRanges = new List<DateRange>();
            for (int i = 0; i < range * 3; i++)
            {
                DateRange dateRange = new DateRange();
                dateRange.Parse(dateTime.ToString("MMMyyyy"));
                dateRanges.Add(dateRange);

                dateTime = dateTime.AddMonths(-1);
            }

            //DateTime dateTime = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);

            //for (int i = 0; i < months; i++)
            //{
            //    DateRange dateRange = new DateRange();
            //    dateRange.Parse(dateTime.AddMonths(-1).ToString("MMMyyyy"));
            //    dateRanges[i] = dateRange;

            //    dateTime = dateTime.AddMonths(-1);
            //}

            return dateRanges.ToArray();
        }
Beispiel #4
0
        public static DateRange[] CreateDateRangesForRecentQuarters(int range)
        {
            DateTime now = DateTime.UtcNow.Date;
            DateRange[] dateRanges = new DateRange[range];
            for (int i = 0; i < range; i++)
            {
                dateRanges[i] = DateRange.CreateQuarterDateRange(now);
                now = now.AddMonths(-3);
            }

            return dateRanges;
        }
Beispiel #5
0
        public static DateRange Create(DateRangeKind dateRangeKind)
        {
            DateTime now = DateTime.UtcNow;

            DateRange dateRange;
            switch (dateRangeKind)
            {
                case DateRangeKind.Today:
                    dateRange = new DateRange(now, now.Date.AddDays(1));
                    break;
                case DateRangeKind.Last7Days:
                    dateRange = Create(DateRangePrecision.Days, -7);
                    break;
                case DateRangeKind.Last30Days:
                    dateRange = Create(DateRangePrecision.Days, -30);
                    break;
                case DateRangeKind.Last90Days:
                    dateRange = Create(DateRangePrecision.Days, -90);
                    break;
                case DateRangeKind.Last6Months:
                    dateRange = Create(DateRangePrecision.Months, -6);
                    break;
                case DateRangeKind.Last12Months:
                    dateRange = Create(DateRangePrecision.Months, -12);
                    break;

                case DateRangeKind.MonthToDate:
                    dateRange = new DateRange(new DateTime(now.Year, now.Month, 1), now.Date.AddDays(1));
                    break;
                case DateRangeKind.QuarterToDate:
                    int quarterMonth = (now.Month % 3) == 0 ? now.Month : 3 * (now.Month / 3) + 1;
                    dateRange = new DateRange(new DateTime(now.Year, quarterMonth, 1), now.Date.AddDays(1));
                    break;
                case DateRangeKind.YearToDate:
                    dateRange = new DateRange(new DateTime(now.Year, 1, 1), now.Date.AddDays(1));
                    break;

                case DateRangeKind.PreviousMonth:
                    DateTime lastMonth = now.AddMonths(-1);
                    dateRange = new DateRange(new DateTime(lastMonth.Year, lastMonth.Month, 1), new DateTime(now.Year, now.Month, 1));
                    break;
                case DateRangeKind.PreviousQuarter:
                    DateTime lastQuarter = now.AddMonths(-3);
                    int lastQuarterMonth = (lastQuarter.Month % 3) == 0 ? lastQuarter.Month : 3 * (lastQuarter.Month / 3) + 1;
                    lastQuarter = new DateTime(lastQuarter.Year, lastQuarterMonth, 1);
                    dateRange = new DateRange(lastQuarter, lastQuarter.AddMonths(3));
                    break;
                case DateRangeKind.PreviousYear:
                    DateTime lastYear = now.AddYears(-1);
                    dateRange = new DateRange(new DateTime(lastYear.Year, lastYear.Month, 1), new DateTime(now.Year, 1, 1));
                    break;
                default:
                    dateRange = DateRange.Empty;
                    break;
            }

            dateRange.Kind = dateRangeKind;

            return dateRange;
        }
Beispiel #6
0
        public static DateRange Create(DateRangePrecision precision, int span, DateTime? dateTime = null)
        {
            DateTime dt = dateTime ?? DateTime.Today;

            DateRange dateRange;
            switch (precision)
            {
                case DateRangePrecision.Seconds:
                    dateRange = new DateRange(dt.Add(new TimeSpan(0, 0, span)), dt);
                    break;
                case DateRangePrecision.Minutes:
                    dateRange = new DateRange(dt.Add(new TimeSpan(0, span, 0)), dt);
                    break;
                case DateRangePrecision.Hours:
                    dateRange = new DateRange(dt.Add(new TimeSpan(span, 0, 0)), dt);
                    break;
                case DateRangePrecision.Days:
                    dateRange = new DateRange(dt.Add(new TimeSpan(span, 0, 0, 0, 0)), dt);
                    break;
                case DateRangePrecision.Months:
                    dateRange = new DateRange(dt.AddMonths(span), dt.AddDays(-1));
                    break;
                case DateRangePrecision.Year:
                    dateRange = new DateRange(dt.AddYears(span), dt);
                    break;
                default:
                    dateRange = DateRange.Empty;
                    break;
            }

            dateRange.Kind = DateRangeKind.Custom;

            return dateRange;
        }