Here, I have described how to get dates between two dates with an interval of days, such the dates of the last 4 weeks, and how to get the start date and end date with a range.
By this method, you can get dates between two dates with an interval of days or you can use months also. In this method, we have to pass three parameters.
- startDate :
- endDate
- intVal
- public static void getDateRange(string startDate, string endDate, int intVal)
- {
- for (DateTime date = DateTime.Parse(startDate); date <= DateTime.Parse(endDate); date = date.AddDays(intVal))
- {
- Console.WriteLine(date.ToString("yyyy-MM-dd"));
- }
- }
In the above method, we will get the dates with an interval of 2 days. Now, call the getDateRange.
- public static void Main()
- {
- getDateRange("2019-01-01", "2019-01-30", 2);
- }
Get The Date Range of the Last 4 Weeks
In this method, we have to pass one parameter, i.e., year, and get the last four-week start date and end date.
- public static void getLast4WeekRanges(int year)
- {
- int dd = int.Parse(DateTime.Now.ToString("dd"));
- int mm = int.Parse(DateTime.Now.ToString("MM"));
- string currentDayinYear = new DateTime(year, mm, dd).ToString("yyyy-MM-dd");
- DayOfWeek dDay = DateTime.Now.DayOfWeek;
- int days = dDay - DayOfWeek.Monday;
- string lastWeekDay;
- if (days == 0)
- {
- lastWeekDay = DateTime.Now.AddDays(-7).ToString("yyyy-MM-dd");
- }
- else
- {
- lastWeekDay = DateTime.Now.AddDays(-days).ToString("yyyy-MM-dd");
- }
-
- string lastWeekEndDay = DateTime.Parse(lastWeekDay).AddDays(6).ToString("yyyy-MM-dd");
- Console.WriteLine("startdate: {0}, enddate: {1}, currentweek: {2}", lastWeekDay, lastWeekEndDay, true);
-
- for (int i = 0; i < 3; i++)
- {
- lastWeekDay = DateTime.Parse(lastWeekDay).AddDays(-7).ToString("yyyy-MM-dd");
- lastWeekEndDay = DateTime.Parse(lastWeekDay).AddDays(6).ToString("yyyy-MM-dd");
- Console.WriteLine("startdate: {0}, enddate: {1}, currentweek: {2}", lastWeekDay, lastWeekEndDay, false);
- }
- }
Now, call the getLast4WeekRanges.
- public static void Main()
- {
- getLast4WeekRanges(2019);
- }
Get start date and end date with a day range
In this method, we will pass a day range like TODAY, LASTWEEK, LASTMONTH, etc and get the start and end date.
- public static void getStartEndDate(string dateRange)
- {
- string startDate, endDate;
- string currentDate = DateTime.Now.ToString("yyyy-MM-dd");
- switch (dateRange)
- {
- case "TODAY":
- startDate = currentDate;
- endDate = currentDate;
- break;
- case "LASTWEEK":
- DayOfWeek desiredDay = DayOfWeek.Monday;
- int offsetAmount = (int)desiredDay - (int)DateTime.Now.DayOfWeek;
- DateTime lastWeekDesiredDay = DateTime.Now.AddDays(-7 + offsetAmount);
- startDate = lastWeekDesiredDay.ToString("yyyy-MM-dd");
- endDate = DateTime.Parse(startDate).AddDays(6).ToString("yyyy-MM-dd");
- break;
- case "LASTMONTH":
- DateTime LastMonthLastDate = DateTime.Now.AddDays(0 - DateTime.Now.Day);
- DateTime LastMonthFirstDate = LastMonthLastDate.AddDays(1 - LastMonthLastDate.Day);
- startDate = LastMonthFirstDate.ToString("yyyy-MM-dd");
- endDate = LastMonthLastDate.ToString("yyyy-MM-dd");
- break;
- case "MONTHLY":
- startDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).ToString("yyyy-MM-dd");
- endDate = currentDate;
- break;
- case "LASTTHREEMONTHS":
- DateTime LastMonthLastDate1 = DateTime.Now.AddDays(0 - DateTime.Now.Day);
- DateTime LastMonthFirstDate1 = LastMonthLastDate1.AddDays(1 - LastMonthLastDate1.Day);
- startDate = LastMonthFirstDate1.AddMonths(-2).ToString("yyyy-MM-dd");
- endDate = LastMonthLastDate1.ToString("yyyy-MM-dd");
- break;
- case "YEARLY":
- startDate = new DateTime(DateTime.Now.Year, 1, 1).ToString("yyyy-MM-dd");
- endDate = currentDate;
- break;
- default:
- startDate = currentDate;
- endDate = currentDate;
- break;
- }
- Console.WriteLine("startdate: {0}, enddate: {1}", startDate, endDate);
- }
Now, call the getStartEndDate.
- public static void Main()
- {
- getStartEndDate("TODAY");
- getStartEndDate("LASTWEEK");
- getStartEndDate("LASTMONTH");
- getStartEndDate("MONTHLY");
- getStartEndDate("LASTTHREEMONTHS");
- getStartEndDate("YEARLY");
- }