Date to Day of the week algorithm?

前端 未结 3 1547
轻奢々
轻奢々 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 12:52

    One of the easiest algorithm for this is Tomohiko Sakamoto Algorithm:

    static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
    y -= m < 3;
    return (y + y/4 - y/100 + y/400 + t[m-1] + d) % 7;
    }
    

    Check this out: https://iq.opengenus.org/tomohiko-sakamoto-algorithm/

    I found Wang's method also interesting

    w = (d - d^(m) + y^ - y* + [y^/4 - y*/2] - 2( c mod 4)) mod 7
    

    http://rmm.ludus-opuscula.org/PDF_Files/Wang_Day_5_8(3_2015)_high.pdf This pdf is really helpful too.

    Thanks!

提交回复
热议问题