Date to Day of the week algorithm?

前端 未结 3 1546
轻奢々
轻奢々 2020-12-20 12:35

What is the algorithm that, given a day, month and year, returns a day of the week?

3条回答
  •  一生所求
    2020-12-20 13:12

    You need a starting point. Today is fine. Hard-code it.

    Then, you need to represent the number of days in a month. This is 31, 28, 31, 30, 31, 30, ... . So you can start adding and subtracting 365 % 7 to the day of the week for each year, and (sum of days in difference of month) % 7 again. And so on.

    The caveat: Leap years occur on every 4th year, but not every 100th, unless that year is also a multiple of 400.

提交回复
热议问题