Answers for "how to get all monday date within two dates c#"

C#
0

Get all dates of every monday between two dates in c#

public static List<DateTime> GetWeekdayInRange(this DateTime from, DateTime to, DayOfWeek day)
    {
        const int daysInWeek = 7;
        var result = new List<DateTime>();
        var daysToAdd = ((int)day - (int)from.DayOfWeek + daysInWeek) % daysInWeek;

        do
        {
            from = from.AddDays(daysToAdd);
            result.Add(from);
            daysToAdd = daysInWeek;
        } while (from < to);

        return result;
    }
Posted by: Guest on July-22-2020
0

how to get all monday date within two dates c#

private static IEnumerable<DateTime> DateIteration(DateTime startDate, DateTime endDate)
{
  for (var date = startDate.Date; date.Date <= endDate.Date; date = date.AddDays(1)) yield
    return date;
}

private static List<DateTime> GetDate(DateTime start, DateTime end, DayOfWeek dow)
{
  var result = new List<DateTime>();
  
  foreach (DateTime day in DateIteration(start, end))
  {
    if (day.DayOfWeek == dow)
    {
      result.Add(day.Date);
    }
  }
  
  return result;
}

public static void Main(string[] args)
{
  DateTime startDate = new DateTime(2021, 10, 1);
  DateTime endDate = new DateTime(2021, 10, 31);
  
  foreach (DateTime date in GetDate(startDate, endDate, DayOfWeek.Monday))
  {
    Console.WriteLine(date);
  }
}
Posted by: Guest on October-16-2021

Code answers related to "how to get all monday date within two dates c#"

C# Answers by Framework

Browse Popular Code Answers by Language