c# - Check if date exist in list of dates -
how can compare mounth , day (not year) list of dates?
datetime[] dates= new datetime[] { new datetime(datetime.now.year, 1, 1), new datetime(datetime.now.year, 5, 1), new datetime(datetime.now.year, 5, 8) }; // result datetime date_1 = new datetime(2016, 1, 1); // ok datetime date_2 = new datetime(2022, 1, 1); // ok datetime date_3 = new datetime(2016, 1, 2); // ko datetime date_4 = new datetime(2016, 1, 3); // ko
you use ienumerable extension any check if array contains date required month , day
datetime date_1 = new datetime(2016, 1, 1); bool exist = dates.any (d => d.month == date_1.month && d.day == date_1.day); console.writeline(exist); datetime date_3 = new datetime(2016, 1, 2); exist = dates.any (d => d.month == date_3.month && d.day == date_3.day); console.writeline(exist);
of course requires using system.linq
Comments
Post a Comment