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:
- var todaysDate = TodaysDate().Date;
Here are the methods.
-
-
- DateTime TodaysDate()
- {
- DateTime date = DateTime.Now;
- return date;
- }
-
-
-
- DateTime PreviousDayDate()
- {
- DateTime date = DateTime.Now.AddDays(-1);
- return date;
- }
-
-
- DateTime QuarterEndDate()
- {
-
- int quarterNumber = (DateTime.Now.Month - 1) / 3 + 1;
- DateTime firstDayOfQuarter = new DateTime(DateTime.Now.Year, (quarterNumber - 1) * 3 + 1, 1);
- DateTime lastDayOfQuarter = firstDayOfQuarter.AddMonths(3).AddDays(-1);
- DateTime date = lastDayOfQuarter;
- return date;
- }
-
-
- DateTime PreviousQuarterEndDate()
- {
- DateTime firstDayOfQuarter;
- DateTime lastDayOfQuarter;
- int quarterNumber = (DateTime.Now.Month - 1) / 3 + 1;
- if (quarterNumber == 1)
- {
- quarterNumber = 4;
- firstDayOfQuarter = new DateTime(DateTime.Now.Year - 1, (quarterNumber - 1) * 3 + 1, 1);
- }
- else
- {
- quarterNumber--;
- firstDayOfQuarter = new DateTime(DateTime.Now.Year, (quarterNumber - 1) * 3 + 1, 1);
- }
- lastDayOfQuarter = firstDayOfQuarter.AddMonths(3).AddDays(-1);
- DateTime date = lastDayOfQuarter;
- return date;
- }
Refer below link to convert DateTime into required String formats: