Thursday, July 12, 2012

Get predifined Date range like Current Month, Last Month, Last year in c#

I reached at a very intersting point while developing a customized Time scale period for a dashboard in Silverlight. Where users have the option to select the Current(Week/Month/Quarter/Year) or  Last(Week/Month/Quarter/Year). I wrote the below code to achieve this.



 public struct DateRange
    {
        public DateTime Start { get; set; }
        public DateTime End { get; set; }
    }

    public static class DateExtension
    {


        public static DateRange ThisYear(this DateTime date)
        {
            DateRange range = new DateRange();

            range.Start = new DateTime(date.Year, 1, 1);
            range.End = range.Start.AddYears(1).AddSeconds(-1);

            return range;
        }

        public static DateRange LastYear(this DateTime date)
        {
            DateRange range = new DateRange();

            range.Start = new DateTime(date.Year - 1, 1, 1);
            range.End = range.Start.AddYears(1).AddSeconds(-1);

            return range;
        }

        public static DateRange ThisMonth(this DateTime date)
        {
            DateRange range = new DateRange();

            range.Start = new DateTime(date.Year, date.Month, 1);
            range.End = range.Start.AddMonths(1).AddSeconds(-1);

            return range;
        }

        public static DateRange LastMonth(this DateTime date)
        {
            DateRange range = new DateRange();
            range.Start = (new DateTime(date.Year, date.Month, 1)).AddMonths(-1);
            range.End = range.Start.AddMonths(1).AddSeconds(-1);
            return range;
        }

        public static DateRange ThisQuarter(this DateTime date)
        {
            //DateTime date;
            int quarterNumber = (date.Month - 1) / 3 + 1;
            DateTime firstDayOfQuarter = new DateTime(date.Year, (quarterNumber - 1) * 3 + 1, 1);
            DateTime lastDayOfQuarter = firstDayOfQuarter.AddMonths(3).AddDays(-1);

            DateRange range = new DateRange();
            range.Start = firstDayOfQuarter;
            range.End = lastDayOfQuarter;

            return range;
        }

        public static DateRange LastQuarter(this DateTime date)
        {
            int quarterNumber = (date.Month - 1) / 3 + 1;
            quarterNumber--;
            DateTime firstDayOfQuarter;
            DateTime lastDayOfQuarter;
            if (quarterNumber >= 0)
            {
                firstDayOfQuarter = new DateTime(date.Year, (quarterNumber - 1) * 3 + 1, 1);
                lastDayOfQuarter = firstDayOfQuarter.AddMonths(3).AddDays(-1);
            }
            else
            {
                /*Else last quareter of previous year*/
                firstDayOfQuarter = new DateTime(date.Year - 1, (4 - 1) * 3 + 1, 1);
                lastDayOfQuarter = firstDayOfQuarter.AddMonths(3).AddDays(-1);
            }
            DateRange range = new DateRange();
            range.Start = firstDayOfQuarter;
            range.End = lastDayOfQuarter;
            return range;
        }


        public static DateRange ThisWeek(this DateTime date)
        {
            DateRange range = new DateRange();

            range.Start = date.Date.AddDays(-(int)date.DayOfWeek);
            range.End = range.Start.AddDays(7).AddSeconds(-1);

            return range;
        }

        public static DateRange LastWeek(this DateTime date)
        {
            DateRange range = ThisWeek(date);

            range.Start = range.Start.AddDays(-7);
            range.End = range.End.AddDays(-7);

            return range;
        }
    }

Feedback or questions are always welcomed.

Cheers!
Vinod

No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog