반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 파이썬 가상환경 설치
- Uipath 설치방법
- Selenium 셀렉터잡기
- 가상환경설치
- 파이썬 가상환경 설치방법
- Python
- 커머스API
- 네이버부동산크롤링
- 네이버매물크롤링
- Uipath 기초
- 파이썬 네이버 로그인
- Element is not clickable at point
- 네이버커머스API
- uipath 입문
- pycdc.exe
- 왕초보 파이썬 실행
- UiPath
- 파이썬 환경설정
- pywinauto
- 네이버 로그인 캡챠해결
- 네이버 로그인 하기
- pycdas.exe
- pywinauto 윈도우
- pywinauto 윈도우제어
- vscode venv 설치
- venv 설치
- 파이썬네이버부동산
- 네이버 로그인 영수증 해결
- 날짜 정규식
- selenium
Archives
- Today
- Total
콘솔워크
python 날짜 n월 n주차 구하기 본문
반응형
규칙
1. 주의 시작은 월요일이다.
2. 월~일 까지 중 월이 다른 경우 어느 월을 선택할지 기준은 더 많은 쪽을 선택하면 된다.
3. 따라서, 더 많은 쪽을 나누는 기준은 "목요일"이다.
예를들어, 위의 규칙으로 2023년 5월 30일은 몇주차 일까?
6월 1주차일까 5월 5주차일까?
정답은 6월 1주차이다.
왜냐하면 목요일이 6월달이기 때문이다.
from datetime import datetime, date, timedelta
import lunardate
import calendar
import numpy as np
last_day_of_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
leap_year_last_day_of_month = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
def get_today_week_name():
today_week_name = ""
today = datetime.now()
year = today.year
month = today.month
day = today.day
x = np.array(calendar.monthcalendar(year, month))
week_of_month = np.where(x == day)[0][0] + 1
first_day_of_month = date(year, month, 1).weekday()
flag = 0
if first_day_of_month > 3:
flag = 1
week_of_month -= flag
if week_of_month == 0:
day_of_week = date(year, month, day).weekday()
if day_of_week > 3: # 목요일
if month == 1:
year -= 1
month = 12
else:
month -= 1
if year % 4 == 0:
day = leap_year_last_day_of_month[month - 1]
else:
day = last_day_of_month[month - 1]
x = np.array(calendar.monthcalendar(year, month))
week_of_month = np.where(x == day)[0][0] + 1
if date(year, month, 1).weekday() > 3:
week_of_month -= 1
else:
week_of_month = 1
elif week_of_month == 5:
if month == 12:
year += 1
month = 1
else:
month += 1
day = 1
day_of_week = date(year, month, day).weekday()
if day_of_week <= 3: # 목요일
week_of_month = 1
if week_of_month == 1:
today_week_name = f"{month}월 첫째 주"
elif week_of_month == 2:
today_week_name = f"{month}월 둘째 주"
elif week_of_month == 3:
today_week_name = f"{month}월 셋째 주"
elif week_of_month == 4:
today_week_name = f"{month}월 넷째 주"
elif week_of_month == 5:
today_week_name = f"{month}월 다섯째 주"
return today_week_name
if __name__ == "__main__":
# Example usage
today_week_name = get_today_week_name()
print(today_week_name)
위의 코드에서 3이랑 비교하는게 많이 나오는데 상수 3은 목요일을 의미한다.
위에 만들어진 get_today_week_name() 은 오늘은 몇월 몇주차인지 결과를 알려주는 함수이다.
오늘이 아니라 특정일이 궁금하다면, 날짜를 파라미터로 받아서 리턴하는 함수로 변경하면 된다.
반응형
'프로그래밍 > python' 카테고리의 다른 글
python selector xpath 크롬 콘솔에서 테스트방법 (0) | 2023.06.01 |
---|---|
python html에서 원하는 태그만 추출하기 (0) | 2023.05.31 |
selenium이나 request로 크롤링을 하는 중 차단을 당한 경우 우회하는 방법 중 하나 (0) | 2023.05.25 |
python, Enum에 접근할 때, 멤버의 이름을 변수로 받아서 사용하는 방법 (0) | 2023.05.22 |
python 인터페이스 만들기 (0) | 2023.05.18 |