DateTime Calculation

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
  1. public static void getDateRange(string startDate, string endDate, int intVal)  
  2. {  
  3.     for (DateTime date = DateTime.Parse(startDate); date <= DateTime.Parse(endDate); date = date.AddDays(intVal))  
  4.     {  
  5.         Console.WriteLine(date.ToString("yyyy-MM-dd"));  
  6.     }  

In the above method, we will get the dates with an interval of 2 days. Now, call the getDateRange.
  1. public static void Main()  
  2. {  
  3.     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.
  1. public static void getLast4WeekRanges(int year)  
  2. {  
  3.     int dd = int.Parse(DateTime.Now.ToString("dd"));  
  4.     int mm = int.Parse(DateTime.Now.ToString("MM"));  
  5.     string currentDayinYear = new DateTime(year, mm, dd).ToString("yyyy-MM-dd");  
  6.     DayOfWeek dDay = DateTime.Now.DayOfWeek;  
  7.     int days = dDay - DayOfWeek.Monday;  
  8.     string lastWeekDay;  
  9.     if (days == 0)  
  10.     {  
  11.         lastWeekDay = DateTime.Now.AddDays(-7).ToString("yyyy-MM-dd");  
  12.     }  
  13.     else  
  14.     {  
  15.         lastWeekDay = DateTime.Now.AddDays(-days).ToString("yyyy-MM-dd");  
  16.     }  
  17.   
  18.     string lastWeekEndDay = DateTime.Parse(lastWeekDay).AddDays(6).ToString("yyyy-MM-dd");  
  19.     Console.WriteLine("startdate: {0}, enddate: {1}, currentweek: {2}", lastWeekDay, lastWeekEndDay, true);  
  20.   
  21.     for (int i = 0; i < 3; i++)  
  22.     {  
  23.         lastWeekDay = DateTime.Parse(lastWeekDay).AddDays(-7).ToString("yyyy-MM-dd");  
  24.         lastWeekEndDay = DateTime.Parse(lastWeekDay).AddDays(6).ToString("yyyy-MM-dd");  
  25.         Console.WriteLine("startdate: {0}, enddate: {1}, currentweek: {2}", lastWeekDay, lastWeekEndDay, false);  
  26.     }  

Now, call the getLast4WeekRanges.
  1. public static void Main()  
  2. {  
  3.     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.
  1. public static void getStartEndDate(string dateRange)  
  2. {  
  3.     string startDate, endDate;  
  4.     string currentDate = DateTime.Now.ToString("yyyy-MM-dd");  
  5.     switch (dateRange)  
  6.     {  
  7.         case "TODAY":  
  8.             startDate = currentDate;  
  9.             endDate = currentDate;  
  10.             break;  
  11.         case "LASTWEEK":  
  12.             DayOfWeek desiredDay = DayOfWeek.Monday;  
  13.             int offsetAmount = (int)desiredDay - (int)DateTime.Now.DayOfWeek;  
  14.             DateTime lastWeekDesiredDay = DateTime.Now.AddDays(-7 + offsetAmount);  
  15.             startDate = lastWeekDesiredDay.ToString("yyyy-MM-dd");  
  16.             endDate = DateTime.Parse(startDate).AddDays(6).ToString("yyyy-MM-dd");  
  17.             break;  
  18.         case "LASTMONTH":  
  19.             DateTime LastMonthLastDate = DateTime.Now.AddDays(0 - DateTime.Now.Day);  
  20.             DateTime LastMonthFirstDate = LastMonthLastDate.AddDays(1 - LastMonthLastDate.Day);  
  21.             startDate = LastMonthFirstDate.ToString("yyyy-MM-dd");  
  22.             endDate = LastMonthLastDate.ToString("yyyy-MM-dd");  
  23.             break;  
  24.         case "MONTHLY":  
  25.             startDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).ToString("yyyy-MM-dd");  
  26.             endDate = currentDate;  
  27.             break;  
  28.         case "LASTTHREEMONTHS":  
  29.             DateTime LastMonthLastDate1 = DateTime.Now.AddDays(0 - DateTime.Now.Day);  
  30.             DateTime LastMonthFirstDate1 = LastMonthLastDate1.AddDays(1 - LastMonthLastDate1.Day);  
  31.             startDate = LastMonthFirstDate1.AddMonths(-2).ToString("yyyy-MM-dd");  
  32.             endDate = LastMonthLastDate1.ToString("yyyy-MM-dd");  
  33.             break;  
  34.         case "YEARLY":  
  35.             startDate = new DateTime(DateTime.Now.Year, 1, 1).ToString("yyyy-MM-dd");  
  36.             endDate = currentDate;  
  37.             break;  
  38.         default:  
  39.             startDate = currentDate;  
  40.             endDate = currentDate;  
  41.             break;  
  42.     }  
  43.     Console.WriteLine("startdate: {0}, enddate: {1}", startDate, endDate);  

 Now, call the getStartEndDate.
  1. public static void Main()  
  2. {  
  3.     getStartEndDate("TODAY");  
  4.     getStartEndDate("LASTWEEK");  
  5.     getStartEndDate("LASTMONTH");  
  6.     getStartEndDate("MONTHLY");  
  7.     getStartEndDate("LASTTHREEMONTHS");  
  8.     getStartEndDate("YEARLY");  
  9. }  
Next Recommended Reading DateTime Format In C#