Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Make Sure All Dates Are Within Same Month and Year

I have a list of dates like this and want to make sure that all dates within that list are within the same month and year. How would I accomplish that?

// Should yield true
IEnumerable<DateTime> dates1 = new List<DateTime>() { new DateTime(2022, 11, 30), new DateTime(2022, 11, 14), new DateTime(2022, 11, 2) };

// Should yield false
IEnumerable<DateTime> dates2 = new List<DateTime>() { new DateTime(2022, 11, 30), new DateTime(2022, 11, 14), new DateTime(2022, 10, 2) };

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can compare them with the first:

DateTime firstDate = dates1.First();
bool allSameMonthAndYear = dates1
    .All(d => d.Year == firstDate.Year && d.Month == firstDate.Month);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading