andrew4582

App Date Helper (Date Ranges)

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