2014-02-16 5 views
1

이 프로그램은 2014 년에 엄격하게 설정됩니다. 그러나 올바른 방향으로 가고 있는지 알고 싶습니다.요일 계산 방법 요일 및 월 제공

def day(d,m): # Function for determining day name for a given date. 
    """Where m is an integer from 1 through 12 expressing a month, and d is an integer from 
    1 through 31 expressing the day-part of a date in 2014.""" 

    day = ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'] 
    weekday = (d + (2.6*m - 0.2) -2*20 + 2014 + (2014//4) + (20//4)) 
    return day[weekday] 
+0

'DateTime'으로 즉시 수정됩니다. 문서를 확인하십시오. 당신이 구조에 날짜를 주면; formatter를 사용하여'day'를 출력 할 수 있습니다. 휠을 재발 명하지 마십시오.) –

답변

1

datetime을 사용할 수 없으면 다음과 같이 작동해야합니다.

def day(d, m): 
    day = (sum((31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)[:m-1]) + d + 3) % 7 
#   ^             ^ ^^^^
#   '---- adding up the days in the months    |  | | | | 
#     up to but not including the current month ----'  | | | | 
#         plus the current day of the month ----' | | | 
#         and the day of the week on 12/31/2013 ----' | | 
#     modulus (%) is what's left over after integer division ----' | 
#              seven days in a week ----' 
7

Don't reinvent the wheel : 이것은 내가 지금까지 무엇을 가지고

>>> import datetime 
>>> datetime.datetime(2014, 2, 16).strftime('%a') 
'Sun' 

Or as a number :

>>> import datetime 
>>> datetime.datetime(2014, 2, 16).weekday() 
6 

당신이 다음 day 목록으로 통과 할 수있는

+1

정보 제공을 위해 임포트 절을 추가하십시오. – markcial

+0

어쨌든 import datetime 사용을 고려했습니다. 아직 해당 섹션을 다루지 않았습니다. 그것을 할 다른 방법이 있습니까? – Yozuru

+0

''datetime import datetime'에서해야 할 클리너. 심지어는 새로운 이름 (더 짧은 이름)을 부여하십시오 : –