Count number of Mondays in a given date range

后端 未结 15 1170
南旧
南旧 2020-11-28 09:35

Given a date range, I need to know how many Mondays (or Tuesdays, Wednesdays, etc) are in that range.

I am currently working in C#.

相关标签:
15条回答
  • 2020-11-28 10:11

    It's fun to look at different algorithm's for calculating day of week, and @Gabe Hollombe's pointing to WP on the subject was a great idea (and I remember implementing Zeller's Congruence in COBOL about twenty years ago), but it was rather along the line of handing someone a blueprint of a clock when all they asked what time it was.

    In C#:

        private int CountMondays(DateTime startDate, DateTime endDate)
        {
            int mondayCount = 0;
    
            for (DateTime dt = startDate; dt < endDate; dt = dt.AddDays(1.0))
            {
                if (dt.DayOfWeek == DayOfWeek.Monday)
                {
                    mondayCount++;
                }
            }
    
            return mondayCount;
        }
    

    This of course does not evaluate the end date for "Mondayness", so if this was desired, make the for loop evaluate

    dt < endDate.AddDays(1.0)
    
    0 讨论(0)
  • 2020-11-28 10:11

    I had a similar problem for a report. I needed the number of workdays between two dates. I could have cycled through the dates and counted but my discrete math training wouldn't let me. Here is a function I wrote in VBA to get the number of workdays between two dates. I'm sure .net has a similar WeekDay function.

       1  
       2  ' WorkDays
       3  ' returns the number of working days between two dates
       4  Public Function WorkDays(ByVal dtBegin As Date, ByVal dtEnd As Date) As Long
       5  
       6     Dim dtFirstSunday As Date
       7     Dim dtLastSaturday As Date
       8     Dim lngWorkDays As Long
       9  
      10     ' get first sunday in range
      11     dtFirstSunday = dtBegin + ((8 - Weekday(dtBegin)) Mod 7)
      12  
      13     ' get last saturday in range
      14     dtLastSaturday = dtEnd - (Weekday(dtEnd) Mod 7)
      15  
      16     ' get work days between first sunday and last saturday
      17     lngWorkDays = (((dtLastSaturday - dtFirstSunday) + 1) / 7) * 5
      18  
      19     ' if first sunday is not begin date
      20     If dtFirstSunday <> dtBegin Then
      21  
      22        ' assume first sunday is after begin date
      23        ' add workdays from begin date to first sunday
      24        lngWorkDays = lngWorkDays + (7 - Weekday(dtBegin))
      25  
      26     End If
      27  
      28     ' if last saturday is not end date
      29     If dtLastSaturday <> dtEnd Then
      30  
      31        ' assume last saturday is before end date
      32        ' add workdays from last saturday to end date
      33        lngWorkDays = lngWorkDays + (Weekday(dtEnd) - 1)
      34  
      35     End If
      36  
      37     ' return working days
      38     WorkDays = lngWorkDays
      39  
      40  End Function
    
    0 讨论(0)
  • 2020-11-28 10:11

    Four years later, I thought I'd run a test:

    [TestMethod]
    public void ShouldFindFridaysInTimeSpan()
    {
        //reference: http://stackoverflow.com/questions/248273/count-number-of-mondays-in-a-given-date-range
    
        var spanOfSixtyDays = new TimeSpan(60, 0, 0, 0);
        var setOfDates = new List<DateTime>(spanOfSixtyDays.Days);
        var now = DateTime.Now;
    
        for(int i = 0; i < spanOfSixtyDays.Days; i++)
        {
            setOfDates.Add(now.AddDays(i));
        }
    
        Assert.IsTrue(setOfDates.Count == 60,
            "The expected number of days is not here.");
    
        var fridays = setOfDates.Where(i => i.DayOfWeek == DayOfWeek.Friday);
    
        Assert.IsTrue(fridays.Count() > 0,
            "The expected Friday days are not here.");
        Assert.IsTrue(fridays.First() == setOfDates.First(i => i.DayOfWeek == DayOfWeek.Friday),
            "The expected first Friday day is not here.");
        Assert.IsTrue(fridays.Last() == setOfDates.Last(i => i.DayOfWeek == DayOfWeek.Friday),
            "The expected last Friday day is not here.");
    }
    

    My use of TimeSpan is a bit of overkill---actually I wanted to query TimeSpan directly.

    0 讨论(0)
提交回复
热议问题