C # Month and day Occurrence between dates

I have a month and a day, and I need to search every month and day in this list between dates. Can someone suggest me to do the best way to get the desired result.

For instance:

Monthly daily list → 01/11, 03/15, 05/25, 09/01

Between dates → 01/01/2012 to 07/01/13

Expected Result:

11/11/2012, 01/11/2013

3/3/2012, 03/15/2013

05/25/2012, 05/25/2013

01/09/2012

I tried like this, but it gives a memory exception, and the time and amount of memory increase if from a date to today, having a gap of several years, is there a quick solution to get it?

DateTime[] dateDates = new DateTime[dateTypeList.Rows.Count]; //convert strings to datetimes: for (int i = 0; i < dateTypeList.Rows.Count; i++) { dateDates[i] = Convert.ToDateTime(Convert.ToDateTime(string.Format("{0}/{1}", Convert.ToInt32(dateTypeList.Rows[i]["Month"]), Convert.ToInt32(dateTypeList.Rows[i]["Day"])))); } //define start and end time: DateTime dateStart = new DateTime(2012, 1, 1); DateTime dateEnd = new DateTime(2013, 7, 1); List<DateTime> results = new List<DateTime>(); //loop days between start and end time: for (DateTime a = dateStart; a <= dateEnd; a.AddDays(1)) { for (int i = 0; i < dateDates.Length; i++) { if (dateDates[i] == a) { results.Add(a); } } } 
+4
source share
4 answers

EDIT: Okay, now I see more about what you are trying to achieve.

So you have an input:

  • Start Date and End Date
  • List of pairs month / day. Currently, you actually do not have this, but that is what you logically received.

Your current code does not take into account the fact that it can be several years.

First modify your data table accordingly:

 var monthDays = dateTypeList.Rows.AsEnumerable() .Select(row => new { Month = row.Field<int>("Month") Day = row.Field<int>("Day") }) .ToList(); 

Now you can use:

 for (int year = startDate.Year; year <= endDate.Year; year++) { foreach (var pair in monthDays) { // Avoid creating a date which doesn't exist... if (!DateTime.IsLeapYear(year) && pair.Month == 2 && pair.Day == 29) { continue; } DateTime date = new DateTime(year, pair.Month, pair.Day); if (date <= startDate && date <= endDate) { results.Add(date); } } } 

This is an immediate problem:

 for (DateTime a = dateStart; a <= dateEnd; a.AddDays(1)) 

DateTime is unchanged, so AddDays does not work unless you use the return value. Do you want to:

 for (DateTime a = dateStart; a <= dateEnd; a = a.AddDays(1)) 

I would also modify this code:

 dateDates[i] = Convert.ToDateTime( Convert.ToDateTime(string.Format("{0}/{1}", Convert.ToInt32(dateTypeList.Rows[i]["Month"]), Convert.ToInt32(dateTypeList.Rows[i]["Day"])))); 
  • This happens via string representation for no reason.
  • It calls Convert.ToDateTime twice for no reason
  • The system date format is assumed to be month / day, which is culture specific.
+3
source
 public class Class1 { public static void Main() { var dayList = new List<customMonthDay>{ new customMonthDay{ Day = 11, Month = 1 }, new customMonthDay{ Day = 15, Month = 3 }, new customMonthDay{ Day = 25, Month = 5 }, new customMonthDay{ Day = 1, Month = 9 } }; var startDate = new DateTime( 2012, 1, 1 ); var endDate = new DateTime( 2013, 7, 1 ); var listYears = getYears(startDate, endDate); var includedDays = new List<customMonthDayYear>(); foreach (var year in listYears) { foreach (var day in dayList) { var candidateday = new customMonthDayYear { Year = year, Month = day.Month, Day = day.Day }; if (candidateday.ToDateTime() > startDate && candidateday.ToDateTime() < endDate) includedDays.Add(candidateday); } } includedDays.ForEach(x => Console.WriteLine(x.ToDateTime().ToString())); } protected static List<int> getYears(DateTime start, DateTime end) { var years = new List<int>(); int diff = end.Year - start.Year; for ( int i = 0; i <= diff; i++ ) { years.Add( start.Year + i ); } return years; } public class customMonthDay { public int Day { get; set; } public int Month { get; set; } } public class customMonthDayYear : customMonthDay { public int Year { get; set; } public DateTime ToDateTime() { return new DateTime(Year, Month, Day); } } } 
+2
source

use the following:

 for each date in list a.Equals(date) or a.CompareTo(date) 
0
source

This works for me, but is there any way to reduce this using linq or something else?

 private List<DateTime> GetDates(DataTable dateTypeList, DateTime fromDate, DateTime toDate) { List<DateTime> results = new List<DateTime>(); fromDate = Convert.ToDateTime(fromDate.ToShortDateString()); toDate=Convert.ToDateTime(toDate.ToShortDateString()); int minDay = fromDate.Day; int minMonth = fromDate.Month; int minYear = fromDate.Year; int maxDay = toDate.Day; int maxMonth = toDate.Month; int maxYear = toDate.Year; for (int j = minYear; j <= maxYear; j++) { for (int i = 0; i < dateTypeList.Rows.Count; i++) { int _day = Convert.ToInt32(dateTypeList.Rows[i]["Day"]); int _month = Convert.ToInt32(dateTypeList.Rows[i]["Month"]); DateTime tempDate = new DateTime(j, _month, _day); if ((tempDate >= fromDate) && (tempDate <= toDate)) { results.Add(tempDate); } } } return dateTypeList; throw new NotImplementedException(); } 
0
source

Source: https://habr.com/ru/post/1444513/


All Articles