C# - Get DateTime for Current Day, Previous Day, Month End, Quarter End, Year End

The below C# code demonstrates how to get DateTime value for different date filters such as Today's Date, Previous Day Date, end of the month, and end of the quarter. 
 
These methods can be called from any program and return a DateTime value. For example: 
  1. var todaysDate =  TodaysDate().Date;   
Here are the methods. 
  1. //Todays Date  
  2.   
  3. DateTime TodaysDate()  
  4. {  
  5.     DateTime date = DateTime.Now;  
  6.     return date;  
  7. }  
  8.   
  9. //Previous Day Date    
  10.   
  11. DateTime PreviousDayDate()  
  12. {  
  13.     DateTime date = DateTime.Now.AddDays(-1);  
  14.     return date;  
  15. }  
  16. //Quarter End Date  
  17.   
  18. DateTime QuarterEndDate()  
  19. {  
  20.   
  21.     int quarterNumber = (DateTime.Now.Month - 1) / 3 + 1;  
  22.     DateTime firstDayOfQuarter = new DateTime(DateTime.Now.Year, (quarterNumber - 1) * 3 + 1, 1);  
  23.     DateTime lastDayOfQuarter = firstDayOfQuarter.AddMonths(3).AddDays(-1);  
  24.     DateTime date = lastDayOfQuarter;  
  25.     return date;  
  26. }  
  27. //Previous Quarter End Date  
  28.   
  29. DateTime PreviousQuarterEndDate()  
  30. {  
  31.     DateTime firstDayOfQuarter;  
  32.     DateTime lastDayOfQuarter;  
  33.     int quarterNumber = (DateTime.Now.Month - 1) / 3 + 1;  
  34.     if (quarterNumber == 1)  
  35.     {  
  36.         quarterNumber = 4;  
  37.         firstDayOfQuarter = new DateTime(DateTime.Now.Year - 1, (quarterNumber - 1) * 3 + 1, 1);  
  38.     }  
  39.     else  
  40.     {  
  41.         quarterNumber--;  
  42.         firstDayOfQuarter = new DateTime(DateTime.Now.Year, (quarterNumber - 1) * 3 + 1, 1);  
  43.     }  
  44.     lastDayOfQuarter = firstDayOfQuarter.AddMonths(3).AddDays(-1);  
  45.     DateTime date = lastDayOfQuarter;  
  46.     return date;  
  47. } 
Refer below link to convert DateTime into required String formats: