How do I get the day of week given a date?

前端 未结 26 1438
迷失自我
迷失自我 2020-11-22 04:40

I want to find out the following: given a date (datetime object), what is the corresponding day of the week?

For instance, Sunday is the first day, Mond

26条回答
  •  礼貌的吻别
    2020-11-22 05:24

    If you have reason to avoid the use of the datetime module, then this function will work.

    Note: The change from the Julian to the Gregorian calendar is assumed to have occurred in 1582. If this is not true for your calendar of interest then change the line if year > 1582: accordingly.

    def dow(year,month,day):
        """ day of week, Sunday = 1, Saturday = 7
         http://en.wikipedia.org/wiki/Zeller%27s_congruence """
        m, q = month, day
        if m == 1:
            m = 13
            year -= 1
        elif m == 2:
            m = 14
            year -= 1
        K = year % 100    
        J = year // 100
        f = (q + int(13*(m + 1)/5.0) + K + int(K/4.0))
        fg = f + int(J/4.0) - 2 * J
        fj = f + 5 - J
        if year > 1582:
            h = fg % 7
        else:
            h = fj % 7
        if h == 0:
            h = 7
        return h
    

提交回复
热议问题