Count number of Mondays in a given date range

后端 未结 15 1308
南旧
南旧 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 09:46

    Convert the dates to Julian Day Number, then do a little bit of math. Since Mondays are zero mod 7, you could do the calculation like this:

    JD1=JulianDayOf(the_first_date)
    JD2=JulianDayOf(the_second_date)
    Round JD1 up to nearest multiple of 7
    Round JD2 up to nearest multiple of 7
    d = JD2-JD1
    nMondays = (JD2-JD1+7)/7    # integer divide
    

提交回复
热议问题