Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace System {
- /// <summary>
- /// The class provides routines to make it easy to work with dates.
- /// </summary>
- ///
- [System.Diagnostics.DebuggerStepThrough()]
- public class DateRangeHelper {
- /// <summary>
- /// Defines a list of date ranges, useful for creating a start and end date on reports.
- /// </summary>
- private static string[] iDateRangeNames =
- {
- /* 0 */ "All",
- /* 1 */ "Today",
- /* 2 */ "This Week",
- /* 3 */ "This Week-to-date",
- /* 4 */ "This Month",
- /* 5 */ "This Month-to-date",
- /* 6 */ "This Quarter",
- /* 7 */ "This Quarter-to-date",
- /* 8 */ "This Year",
- /* 9 */ "This Year-to-date",
- /* 10 */ "Yesterday",
- /* 11 */ "Last Week",
- /* 12 */ "Last Month",
- /* 13 */ "Last Quarter",
- /* 14 */ "Last Year"
- /* 15 */
- };
- private static bool _CalculateDatesWithTime = false;
- public static bool CalculateDatesWithTime {
- get { return _CalculateDatesWithTime; }
- set { _CalculateDatesWithTime = value; }
- }
- public static bool GetDates(string dateRangeName,out DateTime startDate,out DateTime stopDate) {
- bool results = GetDates(dateRangeName,DateTime.Today,out startDate,out stopDate);
- if(results == false)
- return results;
- if(CalculateDatesWithTime == true)
- return results;
- stopDate = stopDate.AddDays(1);
- return results;
- }
- private static bool _GetDates(string dateRangeName,out DateTime startDate,out DateTime stopDate) {
- return GetDates(dateRangeName,DateTime.Today,out startDate,out stopDate);
- }
- /// <summary>
- /// Based on iDateRangeName, returns 2 dates the are set to the correct start and stop dates.
- /// </summary>
- /// <param name="dateRangeName">
- /// Specifies the date range to set to the dates for. Must be one of the DateRangeNames elements.
- /// </param>
- /// <param name="startDate">
- /// On function return, set to the starting date based on dateRangeName.
- /// </param>
- /// <param name="stopDate">
- /// On function return, set to the stopping date based on dateRangeName.
- /// </param>
- public static bool GetDates(string dateRangeName,DateTime todayDate,out DateTime startDate,out DateTime stopDate) {
- DateTime StartDate = todayDate;
- DateTime StopDate = todayDate;
- bool result = true;
- // All.
- if(dateRangeName == DateRangeNames[0]) {
- StartDate = new DateTime(1900,1,1);
- StopDate = new DateTime(2100,12,31);
- }
- // Today.
- if(dateRangeName == DateRangeNames[1]) {
- StartDate = DateTime.Today;
- StopDate = DateTime.Today;
- }
- // This Week.
- if(dateRangeName == DateRangeNames[2]) {
- // Set StartDate to most recent Sunday, first day of the current week.
- DateTime Dt = DateTime.Today;
- while(Dt.DayOfWeek != DayOfWeek.Sunday)
- Dt = Dt.AddDays(-1);
- StartDate = Dt;
- // Set StopDate to the last day of this week, which is Saturday.
- Dt = DateTime.Today;
- while(Dt.DayOfWeek != DayOfWeek.Saturday)
- Dt = Dt.AddDays(1);
- StopDate = Dt;
- }
- // This Week-to-date.
- if(dateRangeName == DateRangeNames[3]) {
- // Set StartDate to most recent Sunday, first day of the current week.
- DateTime Dt = DateTime.Today;
- while(Dt.DayOfWeek != DayOfWeek.Sunday)
- Dt = Dt.AddDays(-1);
- StartDate = Dt;
- // Set StopDate to today.
- StopDate = DateTime.Today;
- }
- // This Month.
- if(dateRangeName == DateRangeNames[4]) {
- // Set StartDate to first of current month.
- StartDate = new DateTime(DateTime.Today.Year,DateTime.Today.Month,1);
- // Set StopDate to last date of current month.
- DateTime Dt = new DateTime(DateTime.Today.Year,DateTime.Today.AddMonths(1).Month,1).AddDays(-1);
- StopDate = new DateTime(DateTime.Today.Year,DateTime.Today.Month,Dt.Day);
- }
- // This Month-to-date.
- if(dateRangeName == DateRangeNames[5]) {
- // Set StartDate to first of current month.
- StartDate = new DateTime(DateTime.Today.Year,DateTime.Today.Month,1);
- // Set StopDate to today.
- StopDate = DateTime.Today;
- }
- // This Quarter.
- if(dateRangeName == DateRangeNames[6]) {
- // Set StartDate to first day of the 1st month of this quarter.
- int CurrentMonth = DateTime.Today.Month;
- DateTime Dt = DateTime.Today;
- while((CurrentMonth % 3) != 0) {
- Dt = Dt.AddMonths(-1);
- CurrentMonth--;
- }
- StartDate = new DateTime(Dt.Year,Dt.Month,1);
- // Set StopDate to last date of current month.
- Dt = StartDate.AddMonths(4).AddDays(-1);
- StopDate = Dt;
- }
- // This Quarter-to-date.
- if(dateRangeName == DateRangeNames[7]) {
- // Set StartDate to first day of the 1st month of this quarter.
- int CurrentMonth = DateTime.Today.Month;
- DateTime Dt = DateTime.Today;
- while((CurrentMonth % 3) != 0) {
- Dt = Dt.AddMonths(-1);
- CurrentMonth--;
- }
- StartDate = new DateTime(Dt.Year,Dt.Month,1);
- // Set StopDate to today.
- StopDate = DateTime.Today;
- }
- // This Year.
- if(dateRangeName == DateRangeNames[8]) {
- // Set StartDate to first day of the year.
- StartDate = new DateTime(DateTime.Today.Year,1,1);
- // Set StopDate to last day of the year.
- StopDate = new DateTime(StartDate.AddYears(1).Year,1,1).AddDays(-1);
- }
- // This Year-to-date.
- if(dateRangeName == DateRangeNames[9]) {
- // Set StartDate to first day of the year.
- StartDate = new DateTime(DateTime.Today.Year,1,1);
- // Set StopDate to today.
- StopDate = DateTime.Today;
- }
- // Yesterday.
- if(dateRangeName == DateRangeNames[10]) {
- // Set StartDate to yesterday.
- StartDate = DateTime.Today.AddDays(-1);
- // Set StopDate to yesterday.
- StopDate = DateTime.Today.AddDays(-1);
- }
- // Last Week.
- if(dateRangeName == DateRangeNames[11]) {
- // Set StartDate to the first day of last week.
- StartDate = DateTime.Today.AddDays(-7);
- while(StartDate.DayOfWeek != System.DayOfWeek.Sunday)
- StartDate = StartDate.AddDays(-1);
- // Set StopDate to the last day of last week, which was last Saturday.
- // If the current day is Saturday, then go to the previous Saturday.
- StopDate = DateTime.Today.AddDays(-1);
- while(StopDate.DayOfWeek != System.DayOfWeek.Saturday)
- StopDate = StopDate.AddDays(-1);
- }
- // Last Month.
- if(dateRangeName == DateRangeNames[12]) {
- // Set StartDate to the first day of last month.
- StartDate = new DateTime(DateTime.Today.AddMonths(-1).Year,DateTime.Today.AddMonths(-1).Month,1);
- // Set StopDate to the last day of last month.
- StopDate = new DateTime(DateTime.Today.Year,DateTime.Today.Month,1).AddDays(-1);
- }
- // Last Quarter.
- if(dateRangeName == DateRangeNames[13]) {
- // Set StartDate to the first day of the first month of the previous quarter.
- int CurrentMonth = DateTime.Today.Month;
- DateTime Dt = DateTime.Today;
- while((CurrentMonth % 3) != 0) {
- Dt = Dt.AddMonths(-1);
- CurrentMonth--;
- }
- StartDate = Dt.AddMonths(-3);
- // Set StopDate to the last day of last month.
- StopDate = StartDate.AddMonths(4).AddDays(-1);
- }
- // Last Year.
- if(dateRangeName == DateRangeNames[14]) {
- // Set StartDate to first day of the last year.
- StartDate = new DateTime(DateTime.Today.AddYears(-1).Year,1,1);
- // Set StopDate to the last day of the last year.
- StopDate = StartDate.AddYears(1).AddDays(-1);
- }
- //// Custom.
- //if (dateRangeName == DateRangeNames[15])
- //{
- // // Don't return anything.
- // result = false;
- //}
- startDate = StartDate;
- stopDate = StopDate;
- return result;
- }
- /// <summary>
- /// Defines a list of date ranges, useful for creating a start and end date on reports.
- /// </summary>
- public static string[] DateRangeNames {
- get {
- return iDateRangeNames;
- }
- }
- /// <summary>
- /// Returns a starting date based on today's date, for the dateRangeName.
- /// </summary>
- public static DateTime GetStartDate(string dateRangeName) {
- DateTime StartDate;
- DateTime StopDate;
- GetDates(dateRangeName,out StartDate,out StopDate);
- return StartDate;
- }
- /// <summary>
- /// Returns a stopping date based on today's date, for the dateRangeName.
- /// </summary>
- public static DateTime GetStopDate(string dateRangeName) {
- DateTime StartDate;
- DateTime StopDate;
- GetDates(dateRangeName,out StartDate,out StopDate);
- return StopDate;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment