public static DateRange getLastDates(string type, string fromdate, string todate) { DateTime date1 = new DateTime(), date2 = new DateTime(); var today = Convert.ToDateTime(GeneralMethod.getdatetime()); DayOfWeek weekStart = DayOfWeek.Monday; // or Sunday, or whenever DateTime startingDate = today; var month = new DateTime(today.Year, today.Month, 1); string str = ""; switch (type) { case "Custom": date1 = Convert.ToDateTime(fromdate); date2 = Convert.ToDateTime(todate); str = "Report From " + date1.ToString("MM/dd/yyyy") + " To " + date2.ToString("MM/dd/yyyy"); break; case "Quarterly": int quarterNumber = (today.Month - 1) / 3 + 1; date1 = new DateTime(today.Year, (quarterNumber - 1) * 3 + 1, 1); date2 = date1.AddMonths(3).AddDays(-1); str = "Quarterly Report (From " + date1.ToString("MM/dd/yyyy") + " To " + date2.ToString("MM/dd/yyyy") + ")"; break; case "Monthly": date1 = month.AddMonths(-1); date2 = month.AddDays(-1); str = "Monthly Report (From " + date1.ToString("MM/dd/yyyy") + " To " + date2.ToString("MM/dd/yyyy") + ")"; break; case "Weekly": while (startingDate.DayOfWeek != weekStart) { startingDate = startingDate.AddDays(-1); } date1 = startingDate.AddDays(-7); date2 = startingDate.AddDays(-1); str = "Weekly Report (From " + date1.ToString("MM/dd/yyyy") + " To " + date2.ToString("MM/dd/yyyy") + ")"; break; case "Biweekly": while (startingDate.DayOfWeek != weekStart) { startingDate = startingDate.AddDays(-1); } date1 = startingDate.AddDays(-14); date2 = startingDate.AddDays(-1); str = "Biweekly Report (From " + date1.ToString("MM/dd/yyyy") + " To " + date2.ToString("MM/dd/yyyy") + ")"; break; case "Current Month": date1 = new DateTime(today.Year, today.Month, 1); date2 = date1.AddMonths(1).AddDays(-1); str = "Current Month Report (From " + date1.ToString("MM/dd/yyyy") + " To " + date2.ToString("MM/dd/yyyy") + ")"; break; case "Next Month": date1 = new DateTime(today.AddMonths(1).Year, today.AddMonths(1).Month, 1); date2 = date1.AddMonths(1).AddDays(-1); str = "Next Month Report (From " + date1.ToString("MM/dd/yyyy") + " To " + date2.ToString("MM/dd/yyyy") + ")"; break; case "Current Week": date1 = today.AddDays(-(int)today.DayOfWeek); date2 = date1.AddDays(7).AddSeconds(-1); str = "Current Week Report (From " + date1.ToString("MM/dd/yyyy") + " To " + date2.ToString("MM/dd/yyyy") + ")"; break; case "Next Week": date1 = today.AddDays(7); date1 = date1.AddDays(-(int)date1.DayOfWeek); date2 = date1.AddDays(7).AddSeconds(-1); str = "Next Week Report (From " + date1.ToString("MM/dd/yyyy") + " To " + date2.ToString("MM/dd/yyyy") + ")"; break; case "Today": date1 = today; date2 = today; str = "Report From " + date1.ToString("MM/dd/yyyy") + " To " + date2.ToString("MM/dd/yyyy") + ""; break; case "All": date1 = Convert.ToDateTime("01/01/1970"); //date2 = today.AddYears(2); date2 = today; str = "Report Date: All"; break; case "AsOf": date1 = Convert.ToDateTime("01/01/1970"); date2 = Convert.ToDateTime(todate); str = "Report As of " + date2.ToString("MM/dd/yyyy"); break; case "AsOfLastMonth": date1 = Convert.ToDateTime("01/01/1970"); date2 = month.AddDays(-1); str = "Report As of Last Month (" + date2.ToString("MM/dd/yyyy") + ")"; break; case "AsOfLastYear": date1 = Convert.ToDateTime("01/01/1970"); date2 = Convert.ToDateTime("12/31/" + (today.Year - 1).ToString() + ""); str = "Report As of Last Year (" + date2.ToString("MM/dd/yyyy") + ")"; break; case "ThisWeektoDate": date1 = today.AddDays(-(int)today.DayOfWeek); date2 = today; str = "This Week to Date Report (From " + date1.ToString("MM/dd/yyyy") + " To " + date2.ToString("MM/dd/yyyy") + ")"; break; case "ThisMonthtoDate": date1 = new DateTime(today.Year, today.Month, 1); date2 = today; str = "This Month to Date Report (From " + date1.ToString("MM/dd/yyyy") + " To " + date2.ToString("MM/dd/yyyy") + ")"; break; case "ThisYeartoDate": date1 = Convert.ToDateTime("01/01/" + (today.Year).ToString() + ""); date2 = today; str = "This Year to Date Report (From " + date1.ToString("MM/dd/yyyy") + " To " + date2.ToString("MM/dd/yyyy") + ")"; break; case "LastYeartoDate": date1 = Convert.ToDateTime("01/01/" + (today.Year - 1).ToString() + ""); date2 = today; str = "This Year to Date Report (From " + date1.ToString("MM/dd/yyyy") + " To " + date2.ToString("MM/dd/yyyy") + ")"; break; case "This Calendar Year": date1 = Convert.ToDateTime("01/01/" + (today.Year).ToString() + ""); date2 = Convert.ToDateTime("12/31/" + (today.Year).ToString() + "");; str = "This Year Report (From " + date1.ToString("MM/dd/yyyy") + " To " + date2.ToString("MM/dd/yyyy") + ")"; break; case "Last Calendar Year": date1 = Convert.ToDateTime("01/01/" + (today.Year - 1).ToString() + ""); date2 = Convert.ToDateTime("12/31/" + (today.Year - 1).ToString() + "");; str = "Last Year Report (From " + date1.ToString("MM/dd/yyyy") + " To " + date2.ToString("MM/dd/yyyy") + ")"; break; } var result = new DateRange { fromdate = date1.ToString("MM/dd/yyyy"), todate = date2.ToString("MM/dd/yyyy"), datetext = str }; return(result); }
public static DateRange getLastDates(string type) { DateTime date1 = new DateTime(), date2 = new DateTime(); var today = Convert.ToDateTime(GeneralMethod.getdatetime()); DayOfWeek weekStart = DayOfWeek.Monday; // or Sunday, or whenever DateTime startingDate = today; switch (type) { case "Quarterly": int quarterNumber = (today.Month - 1) / 3 + 1; date1 = new DateTime(today.Year, (quarterNumber - 1) * 3 + 1, 1); date2 = date1.AddMonths(3).AddDays(-1); break; case "Monthly": var month = new DateTime(today.Year, today.Month, 1); date1 = month.AddMonths(-1); date2 = month.AddDays(-1); break; case "Weekly": while (startingDate.DayOfWeek != weekStart) { startingDate = startingDate.AddDays(-1); } date1 = startingDate.AddDays(-7); date2 = startingDate.AddDays(-1); break; case "Biweekly": while (startingDate.DayOfWeek != weekStart) { startingDate = startingDate.AddDays(-1); } date1 = startingDate.AddDays(-14); date2 = startingDate.AddDays(-1); break; case "Current Month": date1 = new DateTime(today.Year, today.Month, 1); date2 = date1.AddMonths(1).AddDays(-1); break; case "Next Month": date1 = new DateTime(today.AddMonths(1).Year, today.AddMonths(1).Month, 1); date2 = date1.AddMonths(1).AddDays(-1); break; case "Current Week": date1 = today.AddDays(-(int)today.DayOfWeek); date2 = date1.AddDays(7).AddSeconds(-1); break; case "Next Week": date1 = today.AddDays(7); date1 = date1.AddDays(-(int)date1.DayOfWeek); date2 = date1.AddDays(7).AddSeconds(-1); break; } var result = new DateRange { fromdate = date1.ToString("MM/dd/yyyy"), todate = date2.ToString("MM/dd/yyyy") }; return(result); }