Home > Software engineering >  How to get all dates in the last X months
How to get all dates in the last X months

Time:07-05

I want to get all dates in the last three months, so I did the following:

protected void BindPermissions(int empNum)
        {
            var permPeriod = new Dictionary<int, int>();
            permPeriod.Add(DateTime.Now.Year, DateTime.Now.Month);
            permPeriod.Add(DateTime.Now.AddMonths(-1).Year, (DateTime.Now.AddMonths(-1).Month));
            permPeriod.Add(DateTime.Now.AddMonths(-2).Year, (DateTime.Now.AddMonths(-2).Month));
            var dt = payload_object.AttendancePermissionBO.permissionList
                .Where(x => x.empNum == empNum
                && ((x.permDate.Year == permPeriod.Keys.ElementAtOrDefault(0) && x.permDate.Month == permPeriod.Values.ElementAtOrDefault(0)) ||
                (x.permDate.Year == permPeriod.Keys.ElementAtOrDefault(1) && x.permDate.Month == permPeriod.Values.ElementAtOrDefault(1)) ||
                (x.permDate.Year == permPeriod.Keys.ElementAtOrDefault(2) && x.permDate.Month == permPeriod.Values.ElementAtOrDefault(2)))).ToList().OrderBy(x => x.permDate);
            GV_PermissionHistory.DataSource = dt;
            GV_PermissionHistory.DataBind();
        }

Is there a better way to do that or this method suits what i need?

CodePudding user response:

  1. Get the date (boundary) three months ago from today.
DateTime threeMonthsAgoDate = DateTime.Now.AddMonths(-3);
  1. Filter the data for permDate that after (inclusive) the date from 3 months ago.
var dt = payload_object.AttendancePermissionBO.permissionList
    .Where(x => x.empNum == empNum
        && x.permDate >= threeMonthsAgoDate.Date)
    .OrderBy(x => x.permDate)
    .ToList();

CodePudding user response:

You can use the below code for all the dates of last X- months. You can do some changes based on your requirements.

 public static void Main()
    {
        var list= new List<DateTime>();
        
        for(int i=0;i<3;i  ){
            var month= DateTime.Now.AddMonths(-i);
            var monthDates= GetDates(month.Year, month.Month);
            
            list.AddRange(monthDates);
        }
        
        foreach(var item in list){
        Console.WriteLine(item.ToString());
        }
    }
    
public static List<DateTime> GetDates(int year, int month)
{
   return Enumerable.Range(1, DateTime.DaysInMonth(year, month))  
                    .Select(day => new DateTime(year, month, day)) 
                    .ToList(); 
}

CodePudding user response:

It seems like you do not really want all dates in the last three months, but you want all items in your collection where permDate is a date within some date range.

Given your own approach, that some date range seems to be the two previous months plus the entire current month. I.e. for 5th of July 2022, the date range is all of May, June and July 2022.

I think you can simplify your approach by defining a start date and an end date, and compare the permDate values with those two values. A straight-forward way of doing that could be:

var today = DateTime.Today;

var startMonth = today.AddMonths(-2);
var endMonth = today.AddMonths(1);

var startDate = new DateTime(startMonth.Year, startMonth.Month, 1);
var endDate = new DateTime(endMonth.Year, endMonth.Month, 1);

Then, you can use startDate and endDate in your filtering:

var dt = payload_object.AttendancePermissionBO.permissionList
    .Where(x => 
        x.empNum == empNum &&
        x.permDate >= startDate &&
        x.permDate < endDate)
    .OrderBy(x => x.permDate)
    .ToList();
  • Related