andrew4582

DateRangeHelper

Mar 4th, 2012
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.47 KB | None | 0 0
  1. using System;
  2.  
  3. namespace System {
  4.     /// <summary>
  5.     /// The class provides routines to make it easy to work with dates.
  6.     /// </summary>
  7.     ///
  8.     [System.Diagnostics.DebuggerStepThrough()]
  9.     public class DateRangeHelper {
  10.         /// <summary>
  11.         /// Defines a list of date ranges, useful for creating a start and end date on reports.
  12.         /// </summary>
  13.         private static string[] iDateRangeNames =
  14.             {
  15.                 /* 0  */ "All",
  16.                 /* 1  */ "Today",
  17.                 /* 2  */ "This Week",
  18.                 /* 3  */ "This Week-to-date",
  19.                 /* 4  */ "This Month",
  20.                 /* 5  */ "This Month-to-date",
  21.                 /* 6  */ "This Quarter",
  22.                 /* 7  */ "This Quarter-to-date",
  23.                 /* 8  */ "This Year",
  24.                 /* 9  */ "This Year-to-date",
  25.                 /* 10 */ "Yesterday",
  26.                 /* 11 */ "Last Week",
  27.                 /* 12 */ "Last Month",
  28.                 /* 13 */ "Last Quarter",
  29.                 /* 14 */ "Last Year"
  30.                 /* 15 */
  31.             };
  32.  
  33.         private static bool _CalculateDatesWithTime = false;
  34.         public static bool CalculateDatesWithTime {
  35.             get { return _CalculateDatesWithTime; }
  36.             set { _CalculateDatesWithTime = value; }
  37.         }
  38.         public static bool GetDates(string dateRangeName,out DateTime startDate,out DateTime stopDate) {
  39.             bool results = GetDates(dateRangeName,DateTime.Today,out startDate,out stopDate);
  40.             if(results == false)
  41.                 return results;
  42.             if(CalculateDatesWithTime == true)
  43.                 return results;
  44.             stopDate = stopDate.AddDays(1);
  45.             return results;
  46.         }
  47.         private static bool _GetDates(string dateRangeName,out DateTime startDate,out DateTime stopDate) {
  48.             return GetDates(dateRangeName,DateTime.Today,out startDate,out stopDate);
  49.         }
  50.         /// <summary>
  51.         /// Based on iDateRangeName, returns 2 dates the are set to the correct start and stop dates.
  52.         /// </summary>
  53.         /// <param name="dateRangeName">
  54.         /// Specifies the date range to set to the dates for. Must be one of the DateRangeNames elements.
  55.         /// </param>
  56.         /// <param name="startDate">
  57.         /// On function return, set to the starting date based on dateRangeName.
  58.         /// </param>
  59.         /// <param name="stopDate">
  60.         /// On function return, set to the stopping date based on dateRangeName.
  61.         /// </param>
  62.         public static bool GetDates(string dateRangeName,DateTime todayDate,out DateTime startDate,out DateTime stopDate) {
  63.             DateTime StartDate = todayDate;
  64.             DateTime StopDate = todayDate;
  65.             bool result = true;
  66.  
  67.             // All.
  68.             if(dateRangeName == DateRangeNames[0]) {
  69.                 StartDate = new DateTime(1900,1,1);
  70.                 StopDate = new DateTime(2100,12,31);
  71.             }
  72.  
  73.             // Today.
  74.             if(dateRangeName == DateRangeNames[1]) {
  75.                 StartDate = DateTime.Today;
  76.                 StopDate = DateTime.Today;
  77.             }
  78.  
  79.             // This Week.
  80.             if(dateRangeName == DateRangeNames[2]) {
  81.                 // Set StartDate to most recent Sunday, first day of the current week.
  82.                 DateTime Dt = DateTime.Today;
  83.                 while(Dt.DayOfWeek != DayOfWeek.Sunday)
  84.                     Dt = Dt.AddDays(-1);
  85.                 StartDate = Dt;
  86.  
  87.                 // Set StopDate to the last day of this week, which is Saturday.
  88.                 Dt = DateTime.Today;
  89.                 while(Dt.DayOfWeek != DayOfWeek.Saturday)
  90.                     Dt = Dt.AddDays(1);
  91.                 StopDate = Dt;
  92.             }
  93.  
  94.             // This Week-to-date.
  95.             if(dateRangeName == DateRangeNames[3]) {
  96.                 // Set StartDate to most recent Sunday, first day of the current week.
  97.                 DateTime Dt = DateTime.Today;
  98.                 while(Dt.DayOfWeek != DayOfWeek.Sunday)
  99.                     Dt = Dt.AddDays(-1);
  100.                 StartDate = Dt;
  101.  
  102.                 // Set StopDate to today.
  103.                 StopDate = DateTime.Today;
  104.             }
  105.  
  106.             // This Month.
  107.             if(dateRangeName == DateRangeNames[4]) {
  108.                 // Set StartDate to first of current month.
  109.                 StartDate = new DateTime(DateTime.Today.Year,DateTime.Today.Month,1);
  110.  
  111.                 // Set StopDate to last date of current month.
  112.                 DateTime Dt = new DateTime(DateTime.Today.Year,DateTime.Today.AddMonths(1).Month,1).AddDays(-1);
  113.                 StopDate = new DateTime(DateTime.Today.Year,DateTime.Today.Month,Dt.Day);
  114.             }
  115.  
  116.             // This Month-to-date.
  117.             if(dateRangeName == DateRangeNames[5]) {
  118.                 // Set StartDate to first of current month.
  119.                 StartDate = new DateTime(DateTime.Today.Year,DateTime.Today.Month,1);
  120.  
  121.                 // Set StopDate to today.
  122.                 StopDate = DateTime.Today;
  123.             }
  124.  
  125.             // This Quarter.
  126.             if(dateRangeName == DateRangeNames[6]) {
  127.                 // Set StartDate to first day of the 1st month of this quarter.
  128.                 int CurrentMonth = DateTime.Today.Month;
  129.                 DateTime Dt = DateTime.Today;
  130.                 while((CurrentMonth % 3) != 0) {
  131.                     Dt = Dt.AddMonths(-1);
  132.                     CurrentMonth--;
  133.                 }
  134.                 StartDate = new DateTime(Dt.Year,Dt.Month,1);
  135.  
  136.                 // Set StopDate to last date of current month.
  137.                 Dt = StartDate.AddMonths(4).AddDays(-1);
  138.                 StopDate = Dt;
  139.             }
  140.  
  141.             // This Quarter-to-date.
  142.             if(dateRangeName == DateRangeNames[7]) {
  143.                 // Set StartDate to first day of the 1st month of this quarter.
  144.                 int CurrentMonth = DateTime.Today.Month;
  145.                 DateTime Dt = DateTime.Today;
  146.                 while((CurrentMonth % 3) != 0) {
  147.                     Dt = Dt.AddMonths(-1);
  148.                     CurrentMonth--;
  149.                 }
  150.                 StartDate = new DateTime(Dt.Year,Dt.Month,1);
  151.  
  152.                 // Set StopDate to today.
  153.                 StopDate = DateTime.Today;
  154.             }
  155.  
  156.             // This Year.
  157.             if(dateRangeName == DateRangeNames[8]) {
  158.                 // Set StartDate to first day of the year.
  159.                 StartDate = new DateTime(DateTime.Today.Year,1,1);
  160.  
  161.                 // Set StopDate to last day of the year.
  162.                 StopDate = new DateTime(StartDate.AddYears(1).Year,1,1).AddDays(-1);
  163.             }
  164.  
  165.             // This Year-to-date.
  166.             if(dateRangeName == DateRangeNames[9]) {
  167.                 // Set StartDate to first day of the year.
  168.                 StartDate = new DateTime(DateTime.Today.Year,1,1);
  169.  
  170.                 // Set StopDate to today.
  171.                 StopDate = DateTime.Today;
  172.             }
  173.  
  174.             // Yesterday.
  175.             if(dateRangeName == DateRangeNames[10]) {
  176.                 // Set StartDate to yesterday.
  177.                 StartDate = DateTime.Today.AddDays(-1);
  178.  
  179.                 // Set StopDate to yesterday.
  180.                 StopDate = DateTime.Today.AddDays(-1);
  181.             }
  182.  
  183.             // Last Week.
  184.             if(dateRangeName == DateRangeNames[11]) {
  185.                 // Set StartDate to the first day of last week.
  186.                 StartDate = DateTime.Today.AddDays(-7);
  187.                 while(StartDate.DayOfWeek != System.DayOfWeek.Sunday)
  188.                     StartDate = StartDate.AddDays(-1);
  189.  
  190.                 // Set StopDate to the last day of last week, which was last Saturday.
  191.                 // If the current day is Saturday, then go to the previous Saturday.
  192.                 StopDate = DateTime.Today.AddDays(-1);
  193.                 while(StopDate.DayOfWeek != System.DayOfWeek.Saturday)
  194.                     StopDate = StopDate.AddDays(-1);
  195.             }
  196.  
  197.             // Last Month.
  198.             if(dateRangeName == DateRangeNames[12]) {
  199.                 // Set StartDate to the first day of last month.
  200.                 StartDate = new DateTime(DateTime.Today.AddMonths(-1).Year,DateTime.Today.AddMonths(-1).Month,1);
  201.  
  202.                 // Set StopDate to the last day of last month.
  203.                 StopDate = new DateTime(DateTime.Today.Year,DateTime.Today.Month,1).AddDays(-1);
  204.             }
  205.  
  206.             // Last Quarter.
  207.             if(dateRangeName == DateRangeNames[13]) {
  208.                 // Set StartDate to the first day of the first month of the previous quarter.
  209.                 int CurrentMonth = DateTime.Today.Month;
  210.                 DateTime Dt = DateTime.Today;
  211.                 while((CurrentMonth % 3) != 0) {
  212.                     Dt = Dt.AddMonths(-1);
  213.                     CurrentMonth--;
  214.                 }
  215.                 StartDate = Dt.AddMonths(-3);
  216.  
  217.                 // Set StopDate to the last day of last month.
  218.                 StopDate = StartDate.AddMonths(4).AddDays(-1);
  219.             }
  220.  
  221.             // Last Year.
  222.             if(dateRangeName == DateRangeNames[14]) {
  223.                 // Set StartDate to first day of the last year.
  224.                 StartDate = new DateTime(DateTime.Today.AddYears(-1).Year,1,1);
  225.  
  226.                 // Set StopDate to the last day of the last year.
  227.                 StopDate = StartDate.AddYears(1).AddDays(-1);
  228.             }
  229.  
  230.             //// Custom.
  231.             //if (dateRangeName == DateRangeNames[15])
  232.             //{
  233.             //    // Don't return anything.
  234.             //    result = false;
  235.             //}
  236.  
  237.             startDate = StartDate;
  238.             stopDate = StopDate;
  239.  
  240.             return result;
  241.         }
  242.  
  243.         /// <summary>
  244.         /// Defines a list of date ranges, useful for creating a start and end date on reports.
  245.         /// </summary>
  246.         public static string[] DateRangeNames {
  247.             get {
  248.                 return iDateRangeNames;
  249.             }
  250.         }
  251.  
  252.         /// <summary>
  253.         /// Returns a starting date based on today's date, for the dateRangeName.
  254.         /// </summary>
  255.         public static DateTime GetStartDate(string dateRangeName) {
  256.             DateTime StartDate;
  257.             DateTime StopDate;
  258.             GetDates(dateRangeName,out StartDate,out StopDate);
  259.  
  260.             return StartDate;
  261.         }
  262.  
  263.         /// <summary>
  264.         /// Returns a stopping date based on today's date, for the dateRangeName.
  265.         /// </summary>
  266.         public static DateTime GetStopDate(string dateRangeName) {
  267.             DateTime StartDate;
  268.             DateTime StopDate;
  269.             GetDates(dateRangeName,out StartDate,out StopDate);
  270.  
  271.             return StopDate;
  272.         }
  273.     }
  274. }
Advertisement
Add Comment
Please, Sign In to add comment